Annotation of /trunk/Extensions/AssetServer.MySQLAssets/MySQLAssets.cs
Parent Directory
|
Revision Log
Revision 10 - (view) (download)
| 1 : | jhurliman | 10 | using System; |
| 2 : | using System.Collections.Generic; | ||
| 3 : | using System.Net; | ||
| 4 : | using System.Data; | ||
| 5 : | using System.Xml; | ||
| 6 : | using MySql.Data.MySqlClient; | ||
| 7 : | using ExtensionLoader; | ||
| 8 : | using OpenMetaverse; | ||
| 9 : | using OpenMetaverse.StructuredData; | ||
| 10 : | |||
| 11 : | namespace AssetServer.Extensions | ||
| 12 : | { | ||
| 13 : | public static class DBConnection | ||
| 14 : | { | ||
| 15 : | const string CONFIG_FILE = "AssetServer_Config.xml"; | ||
| 16 : | |||
| 17 : | static string connectionString; | ||
| 18 : | |||
| 19 : | public static string ConnectionString | ||
| 20 : | { | ||
| 21 : | get | ||
| 22 : | { | ||
| 23 : | if (connectionString == null) | ||
| 24 : | { | ||
| 25 : | // Parse the connection string out of the OpenSim asset server config file | ||
| 26 : | XmlTextReader reader = new XmlTextReader(CONFIG_FILE); | ||
| 27 : | if (reader.ReadToFollowing("Config")) | ||
| 28 : | { | ||
| 29 : | connectionString = reader.GetAttribute("database_connect"); | ||
| 30 : | |||
| 31 : | // Force connection pooling on | ||
| 32 : | MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder(connectionString); | ||
| 33 : | builder.Pooling = true; | ||
| 34 : | connectionString = builder.ToString(); | ||
| 35 : | } | ||
| 36 : | else | ||
| 37 : | { | ||
| 38 : | connectionString = String.Empty; | ||
| 39 : | Logger.Log(String.Format( | ||
| 40 : | "Cannot find MySQL configuration in {0}, make sure this file is in the same " + | ||
| 41 : | "directory as the asset server and the database_connect attribute is correct", | ||
| 42 : | CONFIG_FILE), Helpers.LogLevel.Error); | ||
| 43 : | } | ||
| 44 : | reader.Close(); | ||
| 45 : | } | ||
| 46 : | |||
| 47 : | return connectionString; | ||
| 48 : | } | ||
| 49 : | } | ||
| 50 : | } | ||
| 51 : | |||
| 52 : | public class MySQLMetadata : IExtension, IMetadataProvider | ||
| 53 : | { | ||
| 54 : | AssetServer server; | ||
| 55 : | |||
| 56 : | public MySQLMetadata(AssetServer server) | ||
| 57 : | { | ||
| 58 : | this.server = server; | ||
| 59 : | } | ||
| 60 : | |||
| 61 : | public void Start() | ||
| 62 : | { | ||
| 63 : | } | ||
| 64 : | |||
| 65 : | public void Stop() | ||
| 66 : | { | ||
| 67 : | } | ||
| 68 : | |||
| 69 : | public void FetchMetadata(UUID assetID, HttpListenerContext context) | ||
| 70 : | { | ||
| 71 : | IDbConnection dbConnection = new MySqlConnection(DBConnection.ConnectionString); | ||
| 72 : | dbConnection.Open(); | ||
| 73 : | |||
| 74 : | IDbCommand command = dbConnection.CreateCommand(); | ||
| 75 : | command.CommandText = String.Format("SELECT assetType,data FROM assets WHERE id='{0}'", assetID.ToString()); | ||
| 76 : | IDataReader reader = command.ExecuteReader(); | ||
| 77 : | |||
| 78 : | if (reader.Read()) | ||
| 79 : | { | ||
| 80 : | AssetType type = (AssetType)reader.GetInt32(0); | ||
| 81 : | byte[] data = (byte[])reader.GetValue(1); | ||
| 82 : | |||
| 83 : | LLSDMap map = new LLSDMap(); | ||
| 84 : | LLSDArray methods = new LLSDArray(); | ||
| 85 : | methods.Add("data"); | ||
| 86 : | map["methods"] = methods; | ||
| 87 : | |||
| 88 : | if (type == AssetType.Texture) | ||
| 89 : | { | ||
| 90 : | // Decode the layer boundaries and return them. This is an expensive operation, | ||
| 91 : | // not fit for a production asset server | ||
| 92 : | AssetTexture texture = new AssetTexture(assetID, data); | ||
| 93 : | texture.DecodeLayerBoundaries(); | ||
| 94 : | |||
| 95 : | LLSDArray layerEnds = new LLSDArray(); | ||
| 96 : | for (int i = 0; i < texture.LayerInfo.Length; i++) | ||
| 97 : | layerEnds.Add(LLSD.FromInteger(texture.LayerInfo[i].End)); | ||
| 98 : | map["layer_ends"] = layerEnds; | ||
| 99 : | } | ||
| 100 : | |||
| 101 : | byte[] responseData = LLSDParser.SerializeXmlBytes(map); | ||
| 102 : | |||
| 103 : | context.Response.StatusCode = (int)HttpStatusCode.OK; | ||
| 104 : | context.Response.ContentType = "application/llsd+xml"; | ||
| 105 : | context.Response.ContentLength64 = responseData.Length; | ||
| 106 : | context.Response.OutputStream.Write(responseData, 0, responseData.Length); | ||
| 107 : | } | ||
| 108 : | else | ||
| 109 : | { | ||
| 110 : | context.Response.StatusCode = (int)HttpStatusCode.NotFound; | ||
| 111 : | } | ||
| 112 : | |||
| 113 : | context.Response.Close(); | ||
| 114 : | } | ||
| 115 : | } | ||
| 116 : | |||
| 117 : | public class MySQLData : IExtension, IDataProvider | ||
| 118 : | { | ||
| 119 : | AssetServer server; | ||
| 120 : | |||
| 121 : | public MySQLData(AssetServer server) | ||
| 122 : | { | ||
| 123 : | this.server = server; | ||
| 124 : | } | ||
| 125 : | |||
| 126 : | public void Start() | ||
| 127 : | { | ||
| 128 : | } | ||
| 129 : | |||
| 130 : | public void Stop() | ||
| 131 : | { | ||
| 132 : | } | ||
| 133 : | |||
| 134 : | public void FetchData(UUID assetID, HttpListenerContext context) | ||
| 135 : | { | ||
| 136 : | IDbConnection dbConnection = new MySqlConnection(DBConnection.ConnectionString); | ||
| 137 : | dbConnection.Open(); | ||
| 138 : | |||
| 139 : | IDbCommand command = dbConnection.CreateCommand(); | ||
| 140 : | command.CommandText = String.Format("SELECT assetType,data FROM assets WHERE id='{0}'", assetID.ToString()); | ||
| 141 : | IDataReader reader = command.ExecuteReader(); | ||
| 142 : | |||
| 143 : | if (reader.Read()) | ||
| 144 : | { | ||
| 145 : | AssetType type = (AssetType)reader.GetInt32(0); | ||
| 146 : | byte[] data = (byte[])reader.GetValue(1); | ||
| 147 : | string extension = type.ToString().ToLower(); | ||
| 148 : | |||
| 149 : | switch (type) | ||
| 150 : | { | ||
| 151 : | case AssetType.Texture: | ||
| 152 : | // RFC 3745 | ||
| 153 : | context.Response.ContentType = "image/jp2"; | ||
| 154 : | extension = "j2c"; | ||
| 155 : | break; | ||
| 156 : | case AssetType.Sound: | ||
| 157 : | // RFC 3534 | ||
| 158 : | context.Response.ContentType = "audio/ogg"; | ||
| 159 : | extension = "ogg"; | ||
| 160 : | break; | ||
| 161 : | default: | ||
| 162 : | context.Response.ContentType = "application/octet-stream"; | ||
| 163 : | break; | ||
| 164 : | } | ||
| 165 : | |||
| 166 : | context.Response.StatusCode = (int)HttpStatusCode.OK; | ||
| 167 : | context.Response.AddHeader("Content-Disposition", "attachment; filename=" + | ||
| 168 : | String.Format("{0}.{1}", assetID, extension)); | ||
| 169 : | context.Response.ContentLength64 = data.Length; | ||
| 170 : | |||
| 171 : | // Send the data | ||
| 172 : | context.Response.OutputStream.Write(data, 0, data.Length); | ||
| 173 : | } | ||
| 174 : | else | ||
| 175 : | { | ||
| 176 : | context.Response.StatusCode = (int)HttpStatusCode.NotFound; | ||
| 177 : | } | ||
| 178 : | |||
| 179 : | context.Response.Close(); | ||
| 180 : | } | ||
| 181 : | } | ||
| 182 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

