Annotation of /trunk/Extensions/Simple.cs
Parent Directory
|
Revision Log
Revision 6 - (view) (download)
| 1 : | jhurliman | 4 | using System; |
| 2 : | using System.Collections.Generic; | ||
| 3 : | using System.Net; | ||
| 4 : | jhurliman | 5 | using System.IO; |
| 5 : | jhurliman | 4 | using ExtensionLoader; |
| 6 : | using OpenMetaverse; | ||
| 7 : | using OpenMetaverse.StructuredData; | ||
| 8 : | jhurliman | 5 | using OpenMetaverse.Imaging; |
| 9 : | jhurliman | 4 | |
| 10 : | namespace AssetServer.Extensions | ||
| 11 : | { | ||
| 12 : | jhurliman | 5 | public struct AssetData |
| 13 : | { | ||
| 14 : | public Asset Asset; | ||
| 15 : | public string Filename; | ||
| 16 : | |||
| 17 : | public AssetData(Asset asset, string filename) | ||
| 18 : | { | ||
| 19 : | Asset = asset; | ||
| 20 : | Filename = filename; | ||
| 21 : | } | ||
| 22 : | } | ||
| 23 : | |||
| 24 : | public static class SimpleDataStorage | ||
| 25 : | { | ||
| 26 : | public const string DEFAULT_DATA_DIR = "DefaultAssets"; | ||
| 27 : | public const string UPLOAD_DATA_DIR = "UploadedAssets"; | ||
| 28 : | public const string TEMP_DATA_DIR = "TemporaryAssets"; | ||
| 29 : | |||
| 30 : | static Dictionary<UUID, AssetData> metadataStorage; | ||
| 31 : | |||
| 32 : | static SimpleDataStorage() | ||
| 33 : | { | ||
| 34 : | metadataStorage = new Dictionary<UUID, AssetData>(); | ||
| 35 : | } | ||
| 36 : | |||
| 37 : | public static void Initialize() | ||
| 38 : | { | ||
| 39 : | LoadFiles(DEFAULT_DATA_DIR, false); | ||
| 40 : | LoadFiles(UPLOAD_DATA_DIR, false); | ||
| 41 : | |||
| 42 : | Logger.Log(String.Format("Initialized the store index with metadata for {0} assets", | ||
| 43 : | metadataStorage.Count), Helpers.LogLevel.Info); | ||
| 44 : | } | ||
| 45 : | |||
| 46 : | public static void StoreAsset(Asset asset) | ||
| 47 : | { | ||
| 48 : | string path; | ||
| 49 : | string filename = String.Format("{0}.{1}", asset.AssetID, asset.AssetType); | ||
| 50 : | |||
| 51 : | if (asset.Temporary) | ||
| 52 : | path = Path.Combine(TEMP_DATA_DIR, filename); | ||
| 53 : | else | ||
| 54 : | path = Path.Combine(UPLOAD_DATA_DIR, filename); | ||
| 55 : | } | ||
| 56 : | |||
| 57 : | jhurliman | 6 | public static bool TryGetAssetData(UUID assetID, out AssetData assetData) |
| 58 : | { | ||
| 59 : | return metadataStorage.TryGetValue(assetID, out assetData); | ||
| 60 : | } | ||
| 61 : | |||
| 62 : | jhurliman | 5 | public static bool TryGetMetadata(UUID assetID, out LLSD metadata) |
| 63 : | { | ||
| 64 : | // FIXME: Generate the LLSD at startup instead of on the fly | ||
| 65 : | AssetData assetData; | ||
| 66 : | if (metadataStorage.TryGetValue(assetID, out assetData)) | ||
| 67 : | { | ||
| 68 : | LLSDMap map = new LLSDMap(); | ||
| 69 : | LLSDArray methods = new LLSDArray(); | ||
| 70 : | methods.Add("data"); | ||
| 71 : | map["methods"] = methods; | ||
| 72 : | |||
| 73 : | if (assetData.Asset is AssetTexture) | ||
| 74 : | { | ||
| 75 : | AssetTexture texture = (AssetTexture)assetData.Asset; | ||
| 76 : | |||
| 77 : | LLSDArray layerEnds = new LLSDArray(); | ||
| 78 : | for (int i = 0; i < texture.LayerInfo.Length; i++) | ||
| 79 : | layerEnds.Add(LLSD.FromInteger(texture.LayerInfo[i].End)); | ||
| 80 : | map["layer_ends"] = layerEnds; | ||
| 81 : | } | ||
| 82 : | |||
| 83 : | metadata = map; | ||
| 84 : | return true; | ||
| 85 : | } | ||
| 86 : | else | ||
| 87 : | { | ||
| 88 : | metadata = null; | ||
| 89 : | return false; | ||
| 90 : | } | ||
| 91 : | } | ||
| 92 : | |||
| 93 : | public static void WipeTemporary() | ||
| 94 : | { | ||
| 95 : | if (Directory.Exists(UPLOAD_DATA_DIR)) | ||
| 96 : | { | ||
| 97 : | try { Directory.Delete(UPLOAD_DATA_DIR); } | ||
| 98 : | catch (Exception ex) { Logger.Log(ex.Message, Helpers.LogLevel.Error, ex); } | ||
| 99 : | } | ||
| 100 : | } | ||
| 101 : | |||
| 102 : | static void LoadFiles(string folder, bool temporary) | ||
| 103 : | { | ||
| 104 : | // Try to create the directory if it doesn't already exist | ||
| 105 : | if (!Directory.Exists(folder)) | ||
| 106 : | { | ||
| 107 : | try { Directory.CreateDirectory(folder); } | ||
| 108 : | catch (Exception ex) | ||
| 109 : | { | ||
| 110 : | Logger.Log(ex.Message, Helpers.LogLevel.Warning, ex); | ||
| 111 : | return; | ||
| 112 : | } | ||
| 113 : | } | ||
| 114 : | |||
| 115 : | lock (metadataStorage) | ||
| 116 : | { | ||
| 117 : | try | ||
| 118 : | { | ||
| 119 : | string[] textures = Directory.GetFiles(folder, "*.Texture", SearchOption.TopDirectoryOnly); | ||
| 120 : | string[] clothing = Directory.GetFiles(folder, "*.Clothing", SearchOption.TopDirectoryOnly); | ||
| 121 : | string[] bodyparts = Directory.GetFiles(folder, "*.Bodypart", SearchOption.TopDirectoryOnly); | ||
| 122 : | string[] sounds = Directory.GetFiles(folder, "*.Sound", SearchOption.TopDirectoryOnly); | ||
| 123 : | |||
| 124 : | for (int i = 0; i < textures.Length; i++) | ||
| 125 : | { | ||
| 126 : | UUID assetID = ParseUUIDFromFilename(textures[i]); | ||
| 127 : | AssetTexture asset = new AssetTexture(assetID, File.ReadAllBytes(textures[i])); | ||
| 128 : | asset.DecodeLayerBoundaries(); | ||
| 129 : | asset.AssetData = null; | ||
| 130 : | asset.Temporary = temporary; | ||
| 131 : | metadataStorage[assetID] = new AssetData(asset, textures[i]); | ||
| 132 : | } | ||
| 133 : | |||
| 134 : | for (int i = 0; i < clothing.Length; i++) | ||
| 135 : | { | ||
| 136 : | UUID assetID = ParseUUIDFromFilename(clothing[i]); | ||
| 137 : | Asset asset = new AssetClothing(assetID, File.ReadAllBytes(clothing[i])); | ||
| 138 : | asset.AssetData = null; | ||
| 139 : | asset.Temporary = temporary; | ||
| 140 : | metadataStorage[assetID] = new AssetData(asset, clothing[i]); | ||
| 141 : | } | ||
| 142 : | |||
| 143 : | for (int i = 0; i < bodyparts.Length; i++) | ||
| 144 : | { | ||
| 145 : | UUID assetID = ParseUUIDFromFilename(bodyparts[i]); | ||
| 146 : | Asset asset = new AssetBodypart(assetID, File.ReadAllBytes(bodyparts[i])); | ||
| 147 : | asset.AssetData = null; | ||
| 148 : | asset.Temporary = temporary; | ||
| 149 : | metadataStorage[assetID] = new AssetData(asset, bodyparts[i]); | ||
| 150 : | } | ||
| 151 : | |||
| 152 : | for (int i = 0; i < sounds.Length; i++) | ||
| 153 : | { | ||
| 154 : | UUID assetID = ParseUUIDFromFilename(sounds[i]); | ||
| 155 : | Asset asset = new AssetSound(assetID, File.ReadAllBytes(sounds[i])); | ||
| 156 : | asset.AssetData = null; | ||
| 157 : | asset.Temporary = true; | ||
| 158 : | metadataStorage[assetID] = new AssetData(asset, sounds[i]); | ||
| 159 : | } | ||
| 160 : | } | ||
| 161 : | catch (Exception ex) | ||
| 162 : | { | ||
| 163 : | Logger.Log(ex.Message, Helpers.LogLevel.Warning, ex); | ||
| 164 : | } | ||
| 165 : | } | ||
| 166 : | } | ||
| 167 : | |||
| 168 : | static UUID ParseUUIDFromFilename(string filename) | ||
| 169 : | { | ||
| 170 : | int dot = filename.LastIndexOf('.'); | ||
| 171 : | |||
| 172 : | if (dot > 35) | ||
| 173 : | { | ||
| 174 : | // Grab the last 36 characters of the filename | ||
| 175 : | string uuidString = filename.Substring(dot - 36, 36); | ||
| 176 : | UUID uuid; | ||
| 177 : | UUID.TryParse(uuidString, out uuid); | ||
| 178 : | return uuid; | ||
| 179 : | } | ||
| 180 : | else | ||
| 181 : | { | ||
| 182 : | return UUID.Zero; | ||
| 183 : | } | ||
| 184 : | } | ||
| 185 : | } | ||
| 186 : | |||
| 187 : | jhurliman | 4 | public class SimpleMetadata : IExtension, IMetadataProvider |
| 188 : | { | ||
| 189 : | AssetServer server; | ||
| 190 : | |||
| 191 : | public SimpleMetadata(AssetServer server) | ||
| 192 : | { | ||
| 193 : | this.server = server; | ||
| 194 : | } | ||
| 195 : | |||
| 196 : | public void Start() | ||
| 197 : | { | ||
| 198 : | } | ||
| 199 : | |||
| 200 : | public void Stop() | ||
| 201 : | { | ||
| 202 : | } | ||
| 203 : | |||
| 204 : | public void FetchMetadata(UUID assetID, HttpListenerContext context) | ||
| 205 : | { | ||
| 206 : | jhurliman | 5 | LLSD response; |
| 207 : | if (SimpleDataStorage.TryGetMetadata(assetID, out response)) | ||
| 208 : | { | ||
| 209 : | byte[] responseData = LLSDParser.SerializeXmlBytes(response); | ||
| 210 : | |||
| 211 : | context.Response.StatusCode = (int)HttpStatusCode.OK; | ||
| 212 : | context.Response.ContentType = "application/xml+llsd"; | ||
| 213 : | context.Response.ContentLength64 = responseData.Length; | ||
| 214 : | context.Response.OutputStream.Write(responseData, 0, responseData.Length); | ||
| 215 : | } | ||
| 216 : | else | ||
| 217 : | { | ||
| 218 : | context.Response.StatusCode = (int)HttpStatusCode.NotFound; | ||
| 219 : | } | ||
| 220 : | |||
| 221 : | context.Response.Close(); | ||
| 222 : | jhurliman | 4 | } |
| 223 : | } | ||
| 224 : | jhurliman | 5 | |
| 225 : | public class SimpleData : IExtension, IDataProvider | ||
| 226 : | { | ||
| 227 : | AssetServer server; | ||
| 228 : | |||
| 229 : | public SimpleData(AssetServer server) | ||
| 230 : | { | ||
| 231 : | this.server = server; | ||
| 232 : | } | ||
| 233 : | |||
| 234 : | public void Start() | ||
| 235 : | { | ||
| 236 : | SimpleDataStorage.Initialize(); | ||
| 237 : | } | ||
| 238 : | |||
| 239 : | public void Stop() | ||
| 240 : | { | ||
| 241 : | SimpleDataStorage.WipeTemporary(); | ||
| 242 : | } | ||
| 243 : | |||
| 244 : | public void FetchData(UUID assetID, HttpListenerContext context) | ||
| 245 : | { | ||
| 246 : | jhurliman | 6 | AssetData assetData; |
| 247 : | if (SimpleDataStorage.TryGetAssetData(assetID, out assetData)) | ||
| 248 : | { | ||
| 249 : | try | ||
| 250 : | { | ||
| 251 : | Stream fileStream = new FileStream(assetData.Filename, FileMode.Open, FileAccess.Read, FileShare.Read); | ||
| 252 : | int dataToRead = (int)fileStream.Length; | ||
| 253 : | |||
| 254 : | string extension = assetData.Asset.AssetType.ToString().ToLower(); | ||
| 255 : | |||
| 256 : | context.Response.StatusCode = (int)HttpStatusCode.OK; | ||
| 257 : | switch (assetData.Asset.AssetType) | ||
| 258 : | { | ||
| 259 : | case AssetType.Texture: | ||
| 260 : | // RFC 3745 | ||
| 261 : | context.Response.ContentType = "image/jp2"; | ||
| 262 : | extension = "j2c"; | ||
| 263 : | break; | ||
| 264 : | case AssetType.Sound: | ||
| 265 : | // RFC 3534 | ||
| 266 : | context.Response.ContentType = "audio/ogg"; | ||
| 267 : | extension = "ogg"; | ||
| 268 : | break; | ||
| 269 : | default: | ||
| 270 : | context.Response.ContentType = "application/octet-stream"; | ||
| 271 : | break; | ||
| 272 : | } | ||
| 273 : | context.Response.AddHeader("Content-Disposition", "attachment; filename=" + | ||
| 274 : | String.Format("{0}.{1}", assetID, extension)); | ||
| 275 : | |||
| 276 : | context.Response.ContentLength64 = dataToRead; | ||
| 277 : | |||
| 278 : | // Stream the file | ||
| 279 : | byte[] buffer = new byte[2048]; | ||
| 280 : | |||
| 281 : | while (dataToRead > 0) | ||
| 282 : | { | ||
| 283 : | // Read the data in buffer | ||
| 284 : | int length = fileStream.Read(buffer, 0, 2048); | ||
| 285 : | |||
| 286 : | // Write the data to the output stream | ||
| 287 : | context.Response.OutputStream.Write(buffer, 0, length); | ||
| 288 : | |||
| 289 : | dataToRead -= length; | ||
| 290 : | } | ||
| 291 : | } | ||
| 292 : | catch (Exception ex) | ||
| 293 : | { | ||
| 294 : | Logger.Log(String.Format("Failed sending data for asset {0} from {1}", assetID, assetData.Filename), | ||
| 295 : | Helpers.LogLevel.Error); | ||
| 296 : | |||
| 297 : | try | ||
| 298 : | { | ||
| 299 : | context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; | ||
| 300 : | context.Response.StatusDescription = ex.Message; | ||
| 301 : | } | ||
| 302 : | catch (Exception) { } | ||
| 303 : | } | ||
| 304 : | } | ||
| 305 : | else | ||
| 306 : | { | ||
| 307 : | context.Response.StatusCode = (int)HttpStatusCode.NotFound; | ||
| 308 : | } | ||
| 309 : | |||
| 310 : | context.Response.Close(); | ||
| 311 : | jhurliman | 5 | } |
| 312 : | } | ||
| 313 : | |||
| 314 : | public class SimpleCreateAsset : IExtension, ICreateAssetProvider | ||
| 315 : | { | ||
| 316 : | AssetServer server; | ||
| 317 : | |||
| 318 : | public SimpleCreateAsset(AssetServer server) | ||
| 319 : | { | ||
| 320 : | this.server = server; | ||
| 321 : | } | ||
| 322 : | |||
| 323 : | public void Start() | ||
| 324 : | { | ||
| 325 : | } | ||
| 326 : | |||
| 327 : | public void Stop() | ||
| 328 : | { | ||
| 329 : | } | ||
| 330 : | |||
| 331 : | public void CreateAsset(LLSD metadata, byte[] assetData, HttpListenerContext context) | ||
| 332 : | { | ||
| 333 : | } | ||
| 334 : | } | ||
| 335 : | jhurliman | 4 | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

