FSAssetService.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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.Diagnostics;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.IO.Compression;
  32. using System.Text;
  33. using System.Threading;
  34. using System.Reflection;
  35. using OpenSim.Data;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Serialization.External;
  38. using OpenSim.Framework.Console;
  39. using OpenSim.Server.Base;
  40. using OpenSim.Services.Base;
  41. using OpenSim.Services.Interfaces;
  42. using Nini.Config;
  43. using log4net;
  44. using OpenMetaverse;
  45. using System.Security.Cryptography;
  46. namespace OpenSim.Services.FSAssetService
  47. {
  48. public class FSAssetConnector : ServiceBase, IAssetService
  49. {
  50. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. static System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  52. static byte[] ToCString(string s)
  53. {
  54. byte[] ret = enc.GetBytes(s);
  55. Array.Resize(ref ret, ret.Length + 1);
  56. ret[ret.Length - 1] = 0;
  57. return ret;
  58. }
  59. protected IAssetLoader m_AssetLoader = null;
  60. protected IFSAssetDataPlugin m_DataConnector = null;
  61. protected IAssetService m_FallbackService;
  62. protected Thread m_WriterThread;
  63. protected Thread m_StatsThread;
  64. protected string m_SpoolDirectory;
  65. protected object m_readLock = new object();
  66. protected object m_statsLock = new object();
  67. protected int m_readCount = 0;
  68. protected int m_readTicks = 0;
  69. protected int m_missingAssets = 0;
  70. protected int m_missingAssetsFS = 0;
  71. protected string m_FSBase;
  72. protected bool m_useOsgridFormat = false;
  73. protected bool m_showStats = true;
  74. private static bool m_Initialized;
  75. private bool m_MainInstance;
  76. public FSAssetConnector(IConfigSource config)
  77. : this(config, "AssetService")
  78. {
  79. }
  80. public FSAssetConnector(IConfigSource config, string configName) : base(config)
  81. {
  82. if (!m_Initialized)
  83. {
  84. m_Initialized = true;
  85. m_MainInstance = true;
  86. MainConsole.Instance.Commands.AddCommand("fs", false,
  87. "show assets", "show assets", "Show asset stats",
  88. HandleShowAssets);
  89. MainConsole.Instance.Commands.AddCommand("fs", false,
  90. "show digest", "show digest <ID>", "Show asset digest",
  91. HandleShowDigest);
  92. MainConsole.Instance.Commands.AddCommand("fs", false,
  93. "delete asset", "delete asset <ID>",
  94. "Delete asset from database",
  95. HandleDeleteAsset);
  96. MainConsole.Instance.Commands.AddCommand("fs", false,
  97. "import", "import <conn> <table> [<start> <count>]",
  98. "Import legacy assets",
  99. HandleImportAssets);
  100. MainConsole.Instance.Commands.AddCommand("fs", false,
  101. "force import", "force import <conn> <table> [<start> <count>]",
  102. "Import legacy assets, overwriting current content",
  103. HandleImportAssets);
  104. }
  105. IConfig assetConfig = config.Configs[configName];
  106. if (assetConfig == null)
  107. throw new Exception("No AssetService configuration");
  108. // Get Database Connector from Asset Config (If present)
  109. string dllName = assetConfig.GetString("StorageProvider", string.Empty);
  110. string connectionString = assetConfig.GetString("ConnectionString", string.Empty);
  111. string realm = assetConfig.GetString("Realm", "fsassets");
  112. int SkipAccessTimeDays = assetConfig.GetInt("DaysBetweenAccessTimeUpdates", 0);
  113. // If not found above, fallback to Database defaults
  114. IConfig dbConfig = config.Configs["DatabaseService"];
  115. if (dbConfig != null)
  116. {
  117. if (dllName == String.Empty)
  118. dllName = dbConfig.GetString("StorageProvider", String.Empty);
  119. if (connectionString == String.Empty)
  120. connectionString = dbConfig.GetString("ConnectionString", String.Empty);
  121. }
  122. // No databse connection found in either config
  123. if (dllName.Equals(String.Empty))
  124. throw new Exception("No StorageProvider configured");
  125. if (connectionString.Equals(String.Empty))
  126. throw new Exception("Missing database connection string");
  127. // Create Storage Provider
  128. m_DataConnector = LoadPlugin<IFSAssetDataPlugin>(dllName);
  129. if (m_DataConnector == null)
  130. throw new Exception(string.Format("Could not find a storage interface in the module {0}", dllName));
  131. // Initialize DB And perform any migrations required
  132. m_DataConnector.Initialise(connectionString, realm, SkipAccessTimeDays);
  133. // Setup Fallback Service
  134. string str = assetConfig.GetString("FallbackService", string.Empty);
  135. if (str != string.Empty)
  136. {
  137. object[] args = new object[] { config };
  138. m_FallbackService = LoadPlugin<IAssetService>(str, args);
  139. if (m_FallbackService != null)
  140. {
  141. m_log.Info("[FSASSETS]: Fallback service loaded");
  142. }
  143. else
  144. {
  145. m_log.Error("[FSASSETS]: Failed to load fallback service");
  146. }
  147. }
  148. // Setup directory structure including temp directory
  149. m_SpoolDirectory = assetConfig.GetString("SpoolDirectory", "/tmp");
  150. string spoolTmp = Path.Combine(m_SpoolDirectory, "spool");
  151. Directory.CreateDirectory(spoolTmp);
  152. m_FSBase = assetConfig.GetString("BaseDirectory", String.Empty);
  153. if (m_FSBase == String.Empty)
  154. {
  155. m_log.ErrorFormat("[FSASSETS]: BaseDirectory not specified");
  156. throw new Exception("Configuration error");
  157. }
  158. m_useOsgridFormat = assetConfig.GetBoolean("UseOsgridFormat", m_useOsgridFormat);
  159. // Default is to show stats to retain original behaviour
  160. m_showStats = assetConfig.GetBoolean("ShowConsoleStats", m_showStats);
  161. if (m_MainInstance)
  162. {
  163. string loader = assetConfig.GetString("DefaultAssetLoader", string.Empty);
  164. if (loader != string.Empty)
  165. {
  166. m_AssetLoader = LoadPlugin<IAssetLoader>(loader);
  167. string loaderArgs = assetConfig.GetString("AssetLoaderArgs", string.Empty);
  168. m_log.InfoFormat("[FSASSETS]: Loading default asset set from {0}", loaderArgs);
  169. m_AssetLoader.ForEachDefaultXmlAsset(loaderArgs,
  170. delegate(AssetBase a)
  171. {
  172. Store(a, false);
  173. });
  174. }
  175. m_WriterThread = new Thread(Writer);
  176. m_WriterThread.Start();
  177. if (m_showStats)
  178. {
  179. m_StatsThread = new Thread(Stats);
  180. m_StatsThread.Start();
  181. }
  182. }
  183. m_log.Info("[FSASSETS]: FS asset service enabled");
  184. }
  185. private void Stats()
  186. {
  187. while (true)
  188. {
  189. Thread.Sleep(60000);
  190. lock (m_statsLock)
  191. {
  192. if (m_readCount > 0)
  193. {
  194. double avg = (double)m_readTicks / (double)m_readCount;
  195. // if (avg > 10000)
  196. // Environment.Exit(0);
  197. m_log.InfoFormat("[FSASSETS]: Read stats: {0} files, {1} ticks, avg {2:F2}, missing {3}, FS {4}", m_readCount, m_readTicks, (double)m_readTicks / (double)m_readCount, m_missingAssets, m_missingAssetsFS);
  198. }
  199. m_readCount = 0;
  200. m_readTicks = 0;
  201. m_missingAssets = 0;
  202. m_missingAssetsFS = 0;
  203. }
  204. }
  205. }
  206. private void Writer()
  207. {
  208. m_log.Info("[ASSET]: Writer started");
  209. while (true)
  210. {
  211. string[] files = Directory.GetFiles(m_SpoolDirectory);
  212. if (files.Length > 0)
  213. {
  214. int tickCount = Environment.TickCount;
  215. for (int i = 0 ; i < files.Length ; i++)
  216. {
  217. string hash = Path.GetFileNameWithoutExtension(files[i]);
  218. string s = HashToFile(hash);
  219. string diskFile = Path.Combine(m_FSBase, s);
  220. bool pathOk = false;
  221. // The cure for chicken bones!
  222. while(true)
  223. {
  224. try
  225. {
  226. // Try to make the directory we need for this file
  227. Directory.CreateDirectory(Path.GetDirectoryName(diskFile));
  228. pathOk = true;
  229. break;
  230. }
  231. catch (System.IO.IOException)
  232. {
  233. // Creating the directory failed. This can't happen unless
  234. // a part of the path already exists as a file. Sadly the
  235. // SRAS data contains such files.
  236. string d = Path.GetDirectoryName(diskFile);
  237. // Test each path component in turn. If we can successfully
  238. // make a directory, the level below must be the chicken bone.
  239. while (d.Length > 0)
  240. {
  241. Console.WriteLine(d);
  242. try
  243. {
  244. Directory.CreateDirectory(Path.GetDirectoryName(d));
  245. }
  246. catch (System.IO.IOException)
  247. {
  248. d = Path.GetDirectoryName(d);
  249. // We failed making the directory and need to
  250. // go up a bit more
  251. continue;
  252. }
  253. // We succeeded in making the directory and (d) is
  254. // the chicken bone
  255. break;
  256. }
  257. // Is the chicken alive?
  258. if (d.Length > 0)
  259. {
  260. Console.WriteLine(d);
  261. FileAttributes attr = File.GetAttributes(d);
  262. if ((attr & FileAttributes.Directory) == 0)
  263. {
  264. // The chicken bone should be resolved.
  265. // Return to writing the file.
  266. File.Delete(d);
  267. continue;
  268. }
  269. }
  270. }
  271. // Could not resolve, skipping
  272. m_log.ErrorFormat("[ASSET]: Could not resolve path creation error for {0}", diskFile);
  273. break;
  274. }
  275. if (pathOk)
  276. {
  277. try
  278. {
  279. byte[] data = File.ReadAllBytes(files[i]);
  280. using (GZipStream gz = new GZipStream(new FileStream(diskFile + ".gz", FileMode.Create), CompressionMode.Compress))
  281. {
  282. gz.Write(data, 0, data.Length);
  283. }
  284. File.Delete(files[i]);
  285. //File.Move(files[i], diskFile);
  286. }
  287. catch(System.IO.IOException e)
  288. {
  289. if (e.Message.StartsWith("Win32 IO returned ERROR_ALREADY_EXISTS"))
  290. File.Delete(files[i]);
  291. else
  292. throw;
  293. }
  294. }
  295. }
  296. int totalTicks = System.Environment.TickCount - tickCount;
  297. if (totalTicks > 0) // Wrap?
  298. {
  299. m_log.InfoFormat("[ASSET]: Write cycle complete, {0} files, {1} ticks, avg {2:F2}", files.Length, totalTicks, (double)totalTicks / (double)files.Length);
  300. }
  301. }
  302. Thread.Sleep(1000);
  303. }
  304. }
  305. string GetSHA256Hash(byte[] data)
  306. {
  307. byte[] hash;
  308. using (SHA256CryptoServiceProvider SHA256 = new SHA256CryptoServiceProvider())
  309. hash = SHA256.ComputeHash(data);
  310. return BitConverter.ToString(hash).Replace("-", String.Empty);
  311. }
  312. public string HashToPath(string hash)
  313. {
  314. if (hash == null || hash.Length < 10)
  315. return "junkyard";
  316. if (m_useOsgridFormat)
  317. {
  318. /*
  319. * The code below is the OSGrid code.
  320. */
  321. return Path.Combine(hash.Substring(0, 3),
  322. Path.Combine(hash.Substring(3, 3)));
  323. }
  324. else
  325. {
  326. /*
  327. * The below is what core would normally use.
  328. * This is modified to work in OSGrid, as seen
  329. * above, because the SRAS data is structured
  330. * that way.
  331. */
  332. return Path.Combine(hash.Substring(0, 2),
  333. Path.Combine(hash.Substring(2, 2),
  334. Path.Combine(hash.Substring(4, 2),
  335. hash.Substring(6, 4))));
  336. }
  337. }
  338. private bool AssetExists(string hash)
  339. {
  340. string s = HashToFile(hash);
  341. string diskFile = Path.Combine(m_FSBase, s);
  342. if (File.Exists(diskFile + ".gz") || File.Exists(diskFile))
  343. return true;
  344. return false;
  345. }
  346. public virtual bool[] AssetsExist(string[] ids)
  347. {
  348. UUID[] uuid = Array.ConvertAll(ids, id => UUID.Parse(id));
  349. return m_DataConnector.AssetsExist(uuid);
  350. }
  351. public string HashToFile(string hash)
  352. {
  353. return Path.Combine(HashToPath(hash), hash);
  354. }
  355. public virtual AssetBase Get(string id)
  356. {
  357. string hash;
  358. return Get(id, out hash);
  359. }
  360. public AssetBase Get(string id, string ForeignAssetService, bool dummy)
  361. {
  362. return null;
  363. }
  364. private AssetBase Get(string id, out string sha)
  365. {
  366. string hash = string.Empty;
  367. int startTime = System.Environment.TickCount;
  368. AssetMetadata metadata;
  369. lock (m_readLock)
  370. {
  371. metadata = m_DataConnector.Get(id, out hash);
  372. }
  373. sha = hash;
  374. if (metadata == null)
  375. {
  376. AssetBase asset = null;
  377. if (m_FallbackService != null)
  378. {
  379. asset = m_FallbackService.Get(id);
  380. if (asset != null)
  381. {
  382. asset.Metadata.ContentType =
  383. SLUtil.SLAssetTypeToContentType((int)asset.Type);
  384. sha = GetSHA256Hash(asset.Data);
  385. m_log.InfoFormat("[FSASSETS]: Added asset {0} from fallback to local store", id);
  386. Store(asset);
  387. }
  388. }
  389. if (asset == null && m_showStats)
  390. {
  391. // m_log.InfoFormat("[FSASSETS]: Asset {0} not found", id);
  392. m_missingAssets++;
  393. }
  394. return asset;
  395. }
  396. AssetBase newAsset = new AssetBase();
  397. newAsset.Metadata = metadata;
  398. try
  399. {
  400. newAsset.Data = GetFsData(hash);
  401. if (newAsset.Data.Length == 0)
  402. {
  403. AssetBase asset = null;
  404. if (m_FallbackService != null)
  405. {
  406. asset = m_FallbackService.Get(id);
  407. if (asset != null)
  408. {
  409. asset.Metadata.ContentType =
  410. SLUtil.SLAssetTypeToContentType((int)asset.Type);
  411. sha = GetSHA256Hash(asset.Data);
  412. m_log.InfoFormat("[FSASSETS]: Added asset {0} from fallback to local store", id);
  413. Store(asset);
  414. }
  415. }
  416. if (asset == null)
  417. {
  418. if (m_showStats)
  419. m_missingAssetsFS++;
  420. // m_log.InfoFormat("[FSASSETS]: Asset {0}, hash {1} not found in FS", id, hash);
  421. }
  422. else
  423. {
  424. // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  425. // Fix bad assets before sending them elsewhere
  426. if (asset.Type == (int)AssetType.Object && asset.Data != null)
  427. {
  428. string xml = ExternalRepresentationUtils.SanitizeXml(Utils.BytesToString(asset.Data));
  429. asset.Data = Utils.StringToBytes(xml);
  430. }
  431. return asset;
  432. }
  433. }
  434. if (m_showStats)
  435. {
  436. lock (m_statsLock)
  437. {
  438. m_readTicks += Environment.TickCount - startTime;
  439. m_readCount++;
  440. }
  441. }
  442. // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  443. // Fix bad assets before sending them elsewhere
  444. if (newAsset.Type == (int)AssetType.Object && newAsset.Data != null)
  445. {
  446. string xml = ExternalRepresentationUtils.SanitizeXml(Utils.BytesToString(newAsset.Data));
  447. newAsset.Data = Utils.StringToBytes(xml);
  448. }
  449. return newAsset;
  450. }
  451. catch (Exception exception)
  452. {
  453. m_log.Error(exception.ToString());
  454. Thread.Sleep(5000);
  455. Environment.Exit(1);
  456. return null;
  457. }
  458. }
  459. public virtual AssetMetadata GetMetadata(string id)
  460. {
  461. string hash;
  462. return m_DataConnector.Get(id, out hash);
  463. }
  464. public virtual byte[] GetData(string id)
  465. {
  466. string hash;
  467. if (m_DataConnector.Get(id, out hash) == null)
  468. return null;
  469. return GetFsData(hash);
  470. }
  471. public bool Get(string id, Object sender, AssetRetrieved handler)
  472. {
  473. AssetBase asset = Get(id);
  474. handler(id, sender, asset);
  475. return true;
  476. }
  477. public byte[] GetFsData(string hash)
  478. {
  479. string spoolFile = Path.Combine(m_SpoolDirectory, hash + ".asset");
  480. if (File.Exists(spoolFile))
  481. {
  482. try
  483. {
  484. byte[] content = File.ReadAllBytes(spoolFile);
  485. return content;
  486. }
  487. catch
  488. {
  489. }
  490. }
  491. string file = HashToFile(hash);
  492. string diskFile = Path.Combine(m_FSBase, file);
  493. if (File.Exists(diskFile + ".gz"))
  494. {
  495. try
  496. {
  497. using (GZipStream gz = new GZipStream(new FileStream(diskFile + ".gz", FileMode.Open, FileAccess.Read), CompressionMode.Decompress))
  498. {
  499. using (MemoryStream ms = new MemoryStream())
  500. {
  501. byte[] data = new byte[32768];
  502. int bytesRead;
  503. do
  504. {
  505. bytesRead = gz.Read(data, 0, 32768);
  506. if (bytesRead > 0)
  507. ms.Write(data, 0, bytesRead);
  508. } while (bytesRead > 0);
  509. return ms.ToArray();
  510. }
  511. }
  512. }
  513. catch (Exception)
  514. {
  515. return new Byte[0];
  516. }
  517. }
  518. else if (File.Exists(diskFile))
  519. {
  520. try
  521. {
  522. byte[] content = File.ReadAllBytes(diskFile);
  523. return content;
  524. }
  525. catch
  526. {
  527. }
  528. }
  529. return new Byte[0];
  530. }
  531. public virtual string Store(AssetBase asset)
  532. {
  533. return Store(asset, false);
  534. }
  535. private string Store(AssetBase asset, bool force)
  536. {
  537. int tickCount = Environment.TickCount;
  538. string hash = GetSHA256Hash(asset.Data);
  539. if (asset.Name.Length > AssetBase.MAX_ASSET_NAME)
  540. {
  541. string assetName = asset.Name.Substring(0, AssetBase.MAX_ASSET_NAME);
  542. m_log.WarnFormat(
  543. "[FSASSETS]: Name '{0}' for asset {1} truncated from {2} to {3} characters on add",
  544. asset.Name, asset.ID, asset.Name.Length, assetName.Length);
  545. asset.Name = assetName;
  546. }
  547. if (asset.Description.Length > AssetBase.MAX_ASSET_DESC)
  548. {
  549. string assetDescription = asset.Description.Substring(0, AssetBase.MAX_ASSET_DESC);
  550. m_log.WarnFormat(
  551. "[FSASSETS]: Description '{0}' for asset {1} truncated from {2} to {3} characters on add",
  552. asset.Description, asset.ID, asset.Description.Length, assetDescription.Length);
  553. asset.Description = assetDescription;
  554. }
  555. if (!AssetExists(hash))
  556. {
  557. string tempFile = Path.Combine(Path.Combine(m_SpoolDirectory, "spool"), hash + ".asset");
  558. string finalFile = Path.Combine(m_SpoolDirectory, hash + ".asset");
  559. if (!File.Exists(finalFile))
  560. {
  561. // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  562. // Fix bad assets before storing on this server
  563. if (asset.Type == (int)AssetType.Object && asset.Data != null)
  564. {
  565. string xml = ExternalRepresentationUtils.SanitizeXml(Utils.BytesToString(asset.Data));
  566. asset.Data = Utils.StringToBytes(xml);
  567. }
  568. FileStream fs = File.Create(tempFile);
  569. fs.Write(asset.Data, 0, asset.Data.Length);
  570. fs.Close();
  571. File.Move(tempFile, finalFile);
  572. }
  573. }
  574. if (asset.ID == string.Empty)
  575. {
  576. if (asset.FullID == UUID.Zero)
  577. {
  578. asset.FullID = UUID.Random();
  579. }
  580. asset.ID = asset.FullID.ToString();
  581. }
  582. else if (asset.FullID == UUID.Zero)
  583. {
  584. UUID uuid = UUID.Zero;
  585. if (UUID.TryParse(asset.ID, out uuid))
  586. {
  587. asset.FullID = uuid;
  588. }
  589. else
  590. {
  591. asset.FullID = UUID.Random();
  592. }
  593. }
  594. if (!m_DataConnector.Store(asset.Metadata, hash))
  595. {
  596. if (asset.Metadata.Type == -2)
  597. return asset.ID;
  598. return UUID.Zero.ToString();
  599. }
  600. else
  601. {
  602. return asset.ID;
  603. }
  604. }
  605. public bool UpdateContent(string id, byte[] data)
  606. {
  607. return false;
  608. // string oldhash;
  609. // AssetMetadata meta = m_DataConnector.Get(id, out oldhash);
  610. //
  611. // if (meta == null)
  612. // return false;
  613. //
  614. // AssetBase asset = new AssetBase();
  615. // asset.Metadata = meta;
  616. // asset.Data = data;
  617. //
  618. // Store(asset);
  619. //
  620. // return true;
  621. }
  622. public virtual bool Delete(string id)
  623. {
  624. m_DataConnector.Delete(id);
  625. return true;
  626. }
  627. private void HandleShowAssets(string module, string[] args)
  628. {
  629. int num = m_DataConnector.Count();
  630. MainConsole.Instance.Output(string.Format("Total asset count: {0}", num));
  631. }
  632. private void HandleShowDigest(string module, string[] args)
  633. {
  634. if (args.Length < 3)
  635. {
  636. MainConsole.Instance.Output("Syntax: show digest <ID>");
  637. return;
  638. }
  639. string hash;
  640. AssetBase asset = Get(args[2], out hash);
  641. if (asset == null || asset.Data.Length == 0)
  642. {
  643. MainConsole.Instance.Output("Asset not found");
  644. return;
  645. }
  646. int i;
  647. MainConsole.Instance.Output(String.Format("Name: {0}", asset.Name));
  648. MainConsole.Instance.Output(String.Format("Description: {0}", asset.Description));
  649. MainConsole.Instance.Output(String.Format("Type: {0}", asset.Type));
  650. MainConsole.Instance.Output(String.Format("Content-type: {0}", asset.Metadata.ContentType));
  651. MainConsole.Instance.Output(String.Format("Flags: {0}", asset.Metadata.Flags.ToString()));
  652. MainConsole.Instance.Output(String.Format("FS file: {0}", HashToFile(hash)));
  653. for (i = 0 ; i < 5 ; i++)
  654. {
  655. int off = i * 16;
  656. if (asset.Data.Length <= off)
  657. break;
  658. int len = 16;
  659. if (asset.Data.Length < off + len)
  660. len = asset.Data.Length - off;
  661. byte[] line = new byte[len];
  662. Array.Copy(asset.Data, off, line, 0, len);
  663. string text = BitConverter.ToString(line);
  664. MainConsole.Instance.Output(String.Format("{0:x4}: {1}", off, text));
  665. }
  666. }
  667. private void HandleDeleteAsset(string module, string[] args)
  668. {
  669. if (args.Length < 3)
  670. {
  671. MainConsole.Instance.Output("Syntax: delete asset <ID>");
  672. return;
  673. }
  674. AssetBase asset = Get(args[2]);
  675. if (asset == null || asset.Data.Length == 0)
  676. {
  677. MainConsole.Instance.Output("Asset not found");
  678. return;
  679. }
  680. m_DataConnector.Delete(args[2]);
  681. MainConsole.Instance.Output("Asset deleted");
  682. }
  683. private void HandleImportAssets(string module, string[] args)
  684. {
  685. bool force = false;
  686. if (args[0] == "force")
  687. {
  688. force = true;
  689. List<string> list = new List<string>(args);
  690. list.RemoveAt(0);
  691. args = list.ToArray();
  692. }
  693. if (args.Length < 3)
  694. {
  695. MainConsole.Instance.Output("Syntax: import <conn> <table> [<start> <count>]");
  696. }
  697. else
  698. {
  699. string conn = args[1];
  700. string table = args[2];
  701. int start = 0;
  702. int count = -1;
  703. if (args.Length > 3)
  704. {
  705. start = Convert.ToInt32(args[3]);
  706. }
  707. if (args.Length > 4)
  708. {
  709. count = Convert.ToInt32(args[4]);
  710. }
  711. m_DataConnector.Import(conn, table, start, count, force, new FSStoreDelegate(Store));
  712. }
  713. }
  714. public AssetBase GetCached(string id)
  715. {
  716. return Get(id);
  717. }
  718. }
  719. }