SimpleAssetStoragePlugin.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Reflection;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using log4net;
  34. namespace OpenSim.Grid.AssetInventoryServer.Plugins.Simple
  35. {
  36. public class SimpleAssetStoragePlugin : IAssetStorageProvider
  37. {
  38. const string EXTENSION_NAME = "SimpleAssetStorage"; // Used in metrics reporting
  39. const string DEFAULT_DATA_DIR = "SimpleAssets";
  40. const string TEMP_DATA_DIR = "SimpleAssetsTemp";
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. AssetInventoryServer server;
  43. Dictionary<UUID, AssetMetadata> metadataStorage;
  44. Dictionary<UUID, string> filenames;
  45. public SimpleAssetStoragePlugin()
  46. {
  47. }
  48. #region Required Interfaces
  49. public BackendResponse TryFetchMetadata(UUID assetID, out AssetMetadata metadata)
  50. {
  51. metadata = null;
  52. BackendResponse ret;
  53. if (metadataStorage.TryGetValue(assetID, out metadata))
  54. ret = BackendResponse.Success;
  55. else
  56. ret = BackendResponse.NotFound;
  57. server.MetricsProvider.LogAssetMetadataFetch(EXTENSION_NAME, ret, assetID, DateTime.Now);
  58. return ret;
  59. }
  60. public BackendResponse TryFetchData(UUID assetID, out byte[] assetData)
  61. {
  62. assetData = null;
  63. string filename;
  64. BackendResponse ret;
  65. if (filenames.TryGetValue(assetID, out filename))
  66. {
  67. try
  68. {
  69. assetData = File.ReadAllBytes(filename);
  70. ret = BackendResponse.Success;
  71. }
  72. catch (Exception ex)
  73. {
  74. m_log.ErrorFormat("[SIMPLEASSETSTORAGE]: Failed reading data for asset {0} from {1}: {2}", assetID, filename, ex.Message);
  75. ret = BackendResponse.Failure;
  76. }
  77. }
  78. else
  79. {
  80. ret = BackendResponse.NotFound;
  81. }
  82. server.MetricsProvider.LogAssetDataFetch(EXTENSION_NAME, ret, assetID, (assetData != null ? assetData.Length : 0), DateTime.Now);
  83. return ret;
  84. }
  85. public BackendResponse TryFetchDataMetadata(UUID assetID, out AssetBase asset)
  86. {
  87. asset = new AssetBase();
  88. AssetMetadata metadata = asset.Metadata;
  89. string filename;
  90. BackendResponse ret;
  91. if (metadataStorage.TryGetValue(assetID, out metadata) &&
  92. filenames.TryGetValue(assetID, out filename))
  93. {
  94. try
  95. {
  96. asset.Data = File.ReadAllBytes(filename);
  97. ret = BackendResponse.Success;
  98. }
  99. catch (Exception ex)
  100. {
  101. m_log.ErrorFormat("[SIMPLEASSETSTORAGE]: Failed reading data for asset {0} from {1}: {2}", assetID, filename, ex.Message);
  102. ret = BackendResponse.Failure;
  103. }
  104. asset.Type = (sbyte) Utils.ContentTypeToSLAssetType(metadata.ContentType);
  105. asset.Local = false;
  106. }
  107. else
  108. {
  109. asset = null;
  110. ret = BackendResponse.NotFound;
  111. }
  112. server.MetricsProvider.LogAssetMetadataFetch(EXTENSION_NAME, ret, assetID, DateTime.Now);
  113. server.MetricsProvider.LogAssetDataFetch(EXTENSION_NAME, ret, assetID, (asset != null && asset.Data != null ? asset.Data.Length : 0), DateTime.Now);
  114. return ret;
  115. }
  116. public BackendResponse TryCreateAsset(AssetBase asset, out UUID assetID)
  117. {
  118. assetID = asset.FullID = UUID.Random();
  119. return TryCreateAsset(asset);
  120. }
  121. public BackendResponse TryCreateAsset(AssetBase asset)
  122. {
  123. BackendResponse ret;
  124. AssetMetadata metadata = asset.Metadata;
  125. string path;
  126. string filename = String.Format("{0}.{1}", asset.FullID, Utils.ContentTypeToExtension(metadata.ContentType));
  127. if (asset.Temporary)
  128. path = Path.Combine(TEMP_DATA_DIR, filename);
  129. else
  130. path = Path.Combine(DEFAULT_DATA_DIR, filename);
  131. try
  132. {
  133. File.WriteAllBytes(path, asset.Data);
  134. lock (filenames) filenames[asset.FullID] = path;
  135. // Set the creation date to right now
  136. metadata.CreationDate = DateTime.Now;
  137. lock (metadataStorage)
  138. metadataStorage[asset.FullID] = metadata;
  139. ret = BackendResponse.Success;
  140. }
  141. catch (Exception ex)
  142. {
  143. m_log.ErrorFormat("[SIMPLEASSETSTORAGE]: Failed writing data for asset {0} to {1}: {2}", asset.FullID, filename, ex.Message);
  144. ret = BackendResponse.Failure;
  145. }
  146. server.MetricsProvider.LogAssetCreate(EXTENSION_NAME, ret, asset.FullID, asset.Data.Length, DateTime.Now);
  147. return ret;
  148. }
  149. public int ForEach(Action<AssetMetadata> action, int start, int count)
  150. {
  151. int rowCount = 0;
  152. //lock (metadataStorage)
  153. //{
  154. // foreach (Metadata metadata in metadataStorage.Values)
  155. // {
  156. // action(metadata);
  157. // ++rowCount;
  158. // }
  159. //}
  160. return rowCount;
  161. }
  162. #endregion Required Interfaces
  163. #region IPlugin implementation
  164. public void Initialise(AssetInventoryServer server)
  165. {
  166. this.server = server;
  167. metadataStorage = new Dictionary<UUID, AssetMetadata>();
  168. filenames = new Dictionary<UUID, string>();
  169. LoadFiles(DEFAULT_DATA_DIR, false);
  170. LoadFiles(TEMP_DATA_DIR, true);
  171. m_log.InfoFormat("[SIMPLEASSETSTORAGE]: Initialized the store index with metadata for {0} assets",
  172. metadataStorage.Count);
  173. }
  174. /// <summary>
  175. /// <para>Initialises asset interface</para>
  176. /// </summary>
  177. public void Initialise()
  178. {
  179. m_log.InfoFormat("[SIMPLEASSETSTORAGE]: {0} cannot be default-initialized!", Name);
  180. throw new PluginNotInitialisedException(Name);
  181. }
  182. public void Dispose()
  183. {
  184. WipeTemporary();
  185. }
  186. public string Version
  187. {
  188. // TODO: this should be something meaningful and not hardcoded?
  189. get { return "0.1"; }
  190. }
  191. public string Name
  192. {
  193. get { return "SimpleAssetStorage"; }
  194. }
  195. #endregion IPlugin implementation
  196. public void WipeTemporary()
  197. {
  198. if (Directory.Exists(TEMP_DATA_DIR))
  199. {
  200. try { Directory.Delete(TEMP_DATA_DIR); }
  201. catch (Exception ex) { m_log.Error("[SIMPLEASSETSTORAGE]: " + ex.Message); }
  202. }
  203. }
  204. void LoadFiles(string folder, bool temporary)
  205. {
  206. // Try to create the directory if it doesn't already exist
  207. if (!Directory.Exists(folder))
  208. {
  209. try { Directory.CreateDirectory(folder); }
  210. catch (Exception ex)
  211. {
  212. m_log.Warn("[SIMPLEASSETSTORAGE]: " + ex.Message);
  213. return;
  214. }
  215. }
  216. lock (metadataStorage)
  217. {
  218. try
  219. {
  220. string[] assets = Directory.GetFiles(folder);
  221. for (int i = 0; i < assets.Length; i++)
  222. {
  223. string filename = assets[i];
  224. byte[] data = File.ReadAllBytes(filename);
  225. AssetMetadata metadata = new AssetMetadata();
  226. metadata.CreationDate = File.GetCreationTime(filename);
  227. metadata.Description = String.Empty;
  228. metadata.FullID = SimpleUtils.ParseUUIDFromFilename(filename);
  229. metadata.Name = SimpleUtils.ParseNameFromFilename(filename);
  230. metadata.SHA1 = OpenMetaverse.Utils.SHA1(data);
  231. metadata.Temporary = false;
  232. metadata.ContentType = Utils.ExtensionToContentType(Path.GetExtension(filename).TrimStart('.'));
  233. // Store the loaded data
  234. metadataStorage[metadata.FullID] = metadata;
  235. filenames[metadata.FullID] = filename;
  236. }
  237. }
  238. catch (Exception ex)
  239. {
  240. m_log.Warn("[SIMPLEASSETSTORAGE]: " + ex.Message);
  241. }
  242. }
  243. }
  244. }
  245. }