Annotation of /trunk/AssetClient/AssetClient.cs
Parent Directory
|
Revision Log
Revision 10 - (view) (download)
| 1 : | jhurliman | 8 | using System; |
| 2 : | using System.Net; | ||
| 3 : | using System.IO; | ||
| 4 : | using OpenMetaverse; | ||
| 5 : | |||
| 6 : | namespace AssetClient | ||
| 7 : | { | ||
| 8 : | public static class AssetClient | ||
| 9 : | { | ||
| 10 : | const string ASSET_SERVER_URL = "http://localhost:9001"; | ||
| 11 : | |||
| 12 : | public static bool TryGetAsset(UUID assetID, out Asset asset) | ||
| 13 : | { | ||
| 14 : | return TryGetAsset(assetID, ASSET_SERVER_URL, out asset); | ||
| 15 : | } | ||
| 16 : | |||
| 17 : | public static bool TryGetAsset(UUID assetID, string assetServerURL, out Asset asset) | ||
| 18 : | { | ||
| 19 : | asset = null; | ||
| 20 : | assetServerURL = assetServerURL.TrimEnd('/'); | ||
| 21 : | string requestURL = String.Format("{0}/{1}/data", assetServerURL, assetID); | ||
| 22 : | |||
| 23 : | try | ||
| 24 : | { | ||
| 25 : | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL); | ||
| 26 : | HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | ||
| 27 : | |||
| 28 : | if (response.StatusCode == HttpStatusCode.OK) | ||
| 29 : | { | ||
| 30 : | int contentLength = (int)response.ContentLength; | ||
| 31 : | byte[] assetData = new byte[contentLength]; | ||
| 32 : | BinaryReader reader = new BinaryReader(response.GetResponseStream()); | ||
| 33 : | int pos = 0; | ||
| 34 : | |||
| 35 : | // Loop until we have read all of the bytes from the response stream | ||
| 36 : | while (pos < contentLength) | ||
| 37 : | pos += reader.Read(assetData, pos, contentLength - pos); | ||
| 38 : | |||
| 39 : | reader.Close(); | ||
| 40 : | |||
| 41 : | switch (response.ContentType) | ||
| 42 : | { | ||
| 43 : | case "image/jp2": | ||
| 44 : | asset = new AssetTexture(assetID, assetData); | ||
| 45 : | if (((AssetTexture)asset).DecodeLayerBoundaries()) | ||
| 46 : | return true; | ||
| 47 : | else | ||
| 48 : | Logger.Log("Failed to decode texture layer boundaries", Helpers.LogLevel.Error); | ||
| 49 : | break; | ||
| 50 : | case "audio/ogg": | ||
| 51 : | asset = new AssetSound(assetID, assetData); | ||
| 52 : | return true; | ||
| 53 : | default: | ||
| 54 : | Logger.Log("Unhandled asset content type: " + response.ContentType, Helpers.LogLevel.Error); | ||
| 55 : | break; | ||
| 56 : | } | ||
| 57 : | } | ||
| 58 : | } | ||
| 59 : | catch (Exception ex) | ||
| 60 : | { | ||
| 61 : | Logger.Log(ex.Message, Helpers.LogLevel.Error, ex); | ||
| 62 : | } | ||
| 63 : | |||
| 64 : | return false; | ||
| 65 : | } | ||
| 66 : | |||
| 67 : | public static bool TryGetMetadata(UUID assetID, out byte[] metadata) | ||
| 68 : | { | ||
| 69 : | return TryGetMetadata(assetID, ASSET_SERVER_URL, out metadata); | ||
| 70 : | } | ||
| 71 : | |||
| 72 : | public static bool TryGetMetadata(UUID assetID, string assetServerURL, out byte[] metadata) | ||
| 73 : | { | ||
| 74 : | metadata = null; | ||
| 75 : | assetServerURL = assetServerURL.TrimEnd('/'); | ||
| 76 : | string requestURL = String.Format("{0}/{1}/metadata", assetServerURL, assetID); | ||
| 77 : | |||
| 78 : | try | ||
| 79 : | { | ||
| 80 : | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL); | ||
| 81 : | HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | ||
| 82 : | |||
| 83 : | if (response.StatusCode == HttpStatusCode.OK) | ||
| 84 : | { | ||
| 85 : | int contentLength = (int)response.ContentLength; | ||
| 86 : | metadata = new byte[contentLength]; | ||
| 87 : | BinaryReader reader = new BinaryReader(response.GetResponseStream()); | ||
| 88 : | int pos = 0; | ||
| 89 : | |||
| 90 : | // Loop until we have read all of the bytes from the response stream | ||
| 91 : | while (pos < contentLength) | ||
| 92 : | pos += reader.Read(metadata, pos, contentLength - pos); | ||
| 93 : | |||
| 94 : | reader.Close(); | ||
| 95 : | return true; | ||
| 96 : | } | ||
| 97 : | } | ||
| 98 : | catch (Exception ex) | ||
| 99 : | { | ||
| 100 : | Logger.Log(ex.Message, Helpers.LogLevel.Error, ex); | ||
| 101 : | } | ||
| 102 : | |||
| 103 : | return false; | ||
| 104 : | } | ||
| 105 : | jhurliman | 10 | |
| 106 : | public static UUID CreateAsset(Asset asset) | ||
| 107 : | { | ||
| 108 : | // FIXME: Implement this | ||
| 109 : | return UUID.Zero; | ||
| 110 : | } | ||
| 111 : | jhurliman | 8 | } |
| 112 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

