FSAssetService.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. private AssetBase Get(string id, out string sha)
  361. {
  362. string hash = string.Empty;
  363. int startTime = System.Environment.TickCount;
  364. AssetMetadata metadata;
  365. lock (m_readLock)
  366. {
  367. metadata = m_DataConnector.Get(id, out hash);
  368. }
  369. sha = hash;
  370. if (metadata == null)
  371. {
  372. AssetBase asset = null;
  373. if (m_FallbackService != null)
  374. {
  375. asset = m_FallbackService.Get(id);
  376. if (asset != null)
  377. {
  378. asset.Metadata.ContentType =
  379. SLUtil.SLAssetTypeToContentType((int)asset.Type);
  380. sha = GetSHA256Hash(asset.Data);
  381. m_log.InfoFormat("[FSASSETS]: Added asset {0} from fallback to local store", id);
  382. Store(asset);
  383. }
  384. }
  385. if (asset == null && m_showStats)
  386. {
  387. // m_log.InfoFormat("[FSASSETS]: Asset {0} not found", id);
  388. m_missingAssets++;
  389. }
  390. return asset;
  391. }
  392. AssetBase newAsset = new AssetBase();
  393. newAsset.Metadata = metadata;
  394. try
  395. {
  396. newAsset.Data = GetFsData(hash);
  397. if (newAsset.Data.Length == 0)
  398. {
  399. AssetBase asset = null;
  400. if (m_FallbackService != null)
  401. {
  402. asset = m_FallbackService.Get(id);
  403. if (asset != null)
  404. {
  405. asset.Metadata.ContentType =
  406. SLUtil.SLAssetTypeToContentType((int)asset.Type);
  407. sha = GetSHA256Hash(asset.Data);
  408. m_log.InfoFormat("[FSASSETS]: Added asset {0} from fallback to local store", id);
  409. Store(asset);
  410. }
  411. }
  412. if (asset == null)
  413. {
  414. if (m_showStats)
  415. m_missingAssetsFS++;
  416. // m_log.InfoFormat("[FSASSETS]: Asset {0}, hash {1} not found in FS", id, hash);
  417. }
  418. else
  419. {
  420. // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  421. // Fix bad assets before sending them elsewhere
  422. if (asset.Type == (int)AssetType.Object && asset.Data != null)
  423. {
  424. string xml = ExternalRepresentationUtils.SanitizeXml(Utils.BytesToString(asset.Data));
  425. asset.Data = Utils.StringToBytes(xml);
  426. }
  427. return asset;
  428. }
  429. }
  430. if (m_showStats)
  431. {
  432. lock (m_statsLock)
  433. {
  434. m_readTicks += Environment.TickCount - startTime;
  435. m_readCount++;
  436. }
  437. }
  438. // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  439. // Fix bad assets before sending them elsewhere
  440. if (newAsset.Type == (int)AssetType.Object && newAsset.Data != null)
  441. {
  442. string xml = ExternalRepresentationUtils.SanitizeXml(Utils.BytesToString(newAsset.Data));
  443. newAsset.Data = Utils.StringToBytes(xml);
  444. }
  445. return newAsset;
  446. }
  447. catch (Exception exception)
  448. {
  449. m_log.Error(exception.ToString());
  450. Thread.Sleep(5000);
  451. Environment.Exit(1);
  452. return null;
  453. }
  454. }
  455. public virtual AssetMetadata GetMetadata(string id)
  456. {
  457. string hash;
  458. return m_DataConnector.Get(id, out hash);
  459. }
  460. public virtual byte[] GetData(string id)
  461. {
  462. string hash;
  463. if (m_DataConnector.Get(id, out hash) == null)
  464. return null;
  465. return GetFsData(hash);
  466. }
  467. public bool Get(string id, Object sender, AssetRetrieved handler)
  468. {
  469. AssetBase asset = Get(id);
  470. handler(id, sender, asset);
  471. return true;
  472. }
  473. public byte[] GetFsData(string hash)
  474. {
  475. string spoolFile = Path.Combine(m_SpoolDirectory, hash + ".asset");
  476. if (File.Exists(spoolFile))
  477. {
  478. try
  479. {
  480. byte[] content = File.ReadAllBytes(spoolFile);
  481. return content;
  482. }
  483. catch
  484. {
  485. }
  486. }
  487. string file = HashToFile(hash);
  488. string diskFile = Path.Combine(m_FSBase, file);
  489. if (File.Exists(diskFile + ".gz"))
  490. {
  491. try
  492. {
  493. using (GZipStream gz = new GZipStream(new FileStream(diskFile + ".gz", FileMode.Open, FileAccess.Read), CompressionMode.Decompress))
  494. {
  495. using (MemoryStream ms = new MemoryStream())
  496. {
  497. byte[] data = new byte[32768];
  498. int bytesRead;
  499. do
  500. {
  501. bytesRead = gz.Read(data, 0, 32768);
  502. if (bytesRead > 0)
  503. ms.Write(data, 0, bytesRead);
  504. } while (bytesRead > 0);
  505. return ms.ToArray();
  506. }
  507. }
  508. }
  509. catch (Exception)
  510. {
  511. return new Byte[0];
  512. }
  513. }
  514. else if (File.Exists(diskFile))
  515. {
  516. try
  517. {
  518. byte[] content = File.ReadAllBytes(diskFile);
  519. return content;
  520. }
  521. catch
  522. {
  523. }
  524. }
  525. return new Byte[0];
  526. }
  527. public virtual string Store(AssetBase asset)
  528. {
  529. return Store(asset, false);
  530. }
  531. private string Store(AssetBase asset, bool force)
  532. {
  533. int tickCount = Environment.TickCount;
  534. string hash = GetSHA256Hash(asset.Data);
  535. if (asset.Name.Length > AssetBase.MAX_ASSET_NAME)
  536. {
  537. string assetName = asset.Name.Substring(0, AssetBase.MAX_ASSET_NAME);
  538. m_log.WarnFormat(
  539. "[FSASSETS]: Name '{0}' for asset {1} truncated from {2} to {3} characters on add",
  540. asset.Name, asset.ID, asset.Name.Length, assetName.Length);
  541. asset.Name = assetName;
  542. }
  543. if (asset.Description.Length > AssetBase.MAX_ASSET_DESC)
  544. {
  545. string assetDescription = asset.Description.Substring(0, AssetBase.MAX_ASSET_DESC);
  546. m_log.WarnFormat(
  547. "[FSASSETS]: Description '{0}' for asset {1} truncated from {2} to {3} characters on add",
  548. asset.Description, asset.ID, asset.Description.Length, assetDescription.Length);
  549. asset.Description = assetDescription;
  550. }
  551. if (!AssetExists(hash))
  552. {
  553. string tempFile = Path.Combine(Path.Combine(m_SpoolDirectory, "spool"), hash + ".asset");
  554. string finalFile = Path.Combine(m_SpoolDirectory, hash + ".asset");
  555. if (!File.Exists(finalFile))
  556. {
  557. // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  558. // Fix bad assets before storing on this server
  559. if (asset.Type == (int)AssetType.Object && asset.Data != null)
  560. {
  561. string xml = ExternalRepresentationUtils.SanitizeXml(Utils.BytesToString(asset.Data));
  562. asset.Data = Utils.StringToBytes(xml);
  563. }
  564. FileStream fs = File.Create(tempFile);
  565. fs.Write(asset.Data, 0, asset.Data.Length);
  566. fs.Close();
  567. File.Move(tempFile, finalFile);
  568. }
  569. }
  570. if (asset.ID == string.Empty)
  571. {
  572. if (asset.FullID == UUID.Zero)
  573. {
  574. asset.FullID = UUID.Random();
  575. }
  576. asset.ID = asset.FullID.ToString();
  577. }
  578. else if (asset.FullID == UUID.Zero)
  579. {
  580. UUID uuid = UUID.Zero;
  581. if (UUID.TryParse(asset.ID, out uuid))
  582. {
  583. asset.FullID = uuid;
  584. }
  585. else
  586. {
  587. asset.FullID = UUID.Random();
  588. }
  589. }
  590. if (!m_DataConnector.Store(asset.Metadata, hash))
  591. {
  592. if (asset.Metadata.Type == -2)
  593. return asset.ID;
  594. return UUID.Zero.ToString();
  595. }
  596. else
  597. {
  598. return asset.ID;
  599. }
  600. }
  601. public bool UpdateContent(string id, byte[] data)
  602. {
  603. return false;
  604. // string oldhash;
  605. // AssetMetadata meta = m_DataConnector.Get(id, out oldhash);
  606. //
  607. // if (meta == null)
  608. // return false;
  609. //
  610. // AssetBase asset = new AssetBase();
  611. // asset.Metadata = meta;
  612. // asset.Data = data;
  613. //
  614. // Store(asset);
  615. //
  616. // return true;
  617. }
  618. public virtual bool Delete(string id)
  619. {
  620. m_DataConnector.Delete(id);
  621. return true;
  622. }
  623. private void HandleShowAssets(string module, string[] args)
  624. {
  625. int num = m_DataConnector.Count();
  626. MainConsole.Instance.Output(string.Format("Total asset count: {0}", num));
  627. }
  628. private void HandleShowDigest(string module, string[] args)
  629. {
  630. if (args.Length < 3)
  631. {
  632. MainConsole.Instance.Output("Syntax: show digest <ID>");
  633. return;
  634. }
  635. string hash;
  636. AssetBase asset = Get(args[2], out hash);
  637. if (asset == null || asset.Data.Length == 0)
  638. {
  639. MainConsole.Instance.Output("Asset not found");
  640. return;
  641. }
  642. int i;
  643. MainConsole.Instance.Output(String.Format("Name: {0}", asset.Name));
  644. MainConsole.Instance.Output(String.Format("Description: {0}", asset.Description));
  645. MainConsole.Instance.Output(String.Format("Type: {0}", asset.Type));
  646. MainConsole.Instance.Output(String.Format("Content-type: {0}", asset.Metadata.ContentType));
  647. MainConsole.Instance.Output(String.Format("Flags: {0}", asset.Metadata.Flags.ToString()));
  648. MainConsole.Instance.Output(String.Format("FS file: {0}", HashToFile(hash)));
  649. for (i = 0 ; i < 5 ; i++)
  650. {
  651. int off = i * 16;
  652. if (asset.Data.Length <= off)
  653. break;
  654. int len = 16;
  655. if (asset.Data.Length < off + len)
  656. len = asset.Data.Length - off;
  657. byte[] line = new byte[len];
  658. Array.Copy(asset.Data, off, line, 0, len);
  659. string text = BitConverter.ToString(line);
  660. MainConsole.Instance.Output(String.Format("{0:x4}: {1}", off, text));
  661. }
  662. }
  663. private void HandleDeleteAsset(string module, string[] args)
  664. {
  665. if (args.Length < 3)
  666. {
  667. MainConsole.Instance.Output("Syntax: delete asset <ID>");
  668. return;
  669. }
  670. AssetBase asset = Get(args[2]);
  671. if (asset == null || asset.Data.Length == 0)
  672. {
  673. MainConsole.Instance.Output("Asset not found");
  674. return;
  675. }
  676. m_DataConnector.Delete(args[2]);
  677. MainConsole.Instance.Output("Asset deleted");
  678. }
  679. private void HandleImportAssets(string module, string[] args)
  680. {
  681. bool force = false;
  682. if (args[0] == "force")
  683. {
  684. force = true;
  685. List<string> list = new List<string>(args);
  686. list.RemoveAt(0);
  687. args = list.ToArray();
  688. }
  689. if (args.Length < 3)
  690. {
  691. MainConsole.Instance.Output("Syntax: import <conn> <table> [<start> <count>]");
  692. }
  693. else
  694. {
  695. string conn = args[1];
  696. string table = args[2];
  697. int start = 0;
  698. int count = -1;
  699. if (args.Length > 3)
  700. {
  701. start = Convert.ToInt32(args[3]);
  702. }
  703. if (args.Length > 4)
  704. {
  705. count = Convert.ToInt32(args[4]);
  706. }
  707. m_DataConnector.Import(conn, table, start, count, force, new FSStoreDelegate(Store));
  708. }
  709. }
  710. public AssetBase GetCached(string id)
  711. {
  712. return Get(id);
  713. }
  714. }
  715. }