AgentAssetTransactions.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 OpenSim 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. //moved to a module, left here until the module is found to have no problems
  28. /*
  29. using System;
  30. using System.Collections.Generic;
  31. using System.IO;
  32. using libsecondlife;
  33. using libsecondlife.Packets;
  34. using OpenSim.Framework.Servers;
  35. using OpenSim.Region.Capabilities;
  36. namespace OpenSim.Framework.Communications.Cache
  37. {
  38. /// <summary>
  39. /// Manage asset transactions for a single agent.
  40. /// </summary>
  41. public class AgentAssetTransactions
  42. {
  43. private static readonly log4net.ILog m_log
  44. = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  45. // Fields
  46. public List<AssetCapsUploader> CapsUploaders = new List<AssetCapsUploader>();
  47. public List<NoteCardCapsUpdate> NotecardUpdaters = new List<NoteCardCapsUpdate>();
  48. public LLUUID UserID;
  49. public Dictionary<LLUUID, AssetXferUploader> XferUploaders = new Dictionary<LLUUID, AssetXferUploader>();
  50. public AgentAssetTransactionsManager Manager;
  51. private bool m_dumpAssetsToFile;
  52. // Methods
  53. public AgentAssetTransactions(LLUUID agentID, AgentAssetTransactionsManager manager, bool dumpAssetsToFile)
  54. {
  55. UserID = agentID;
  56. Manager = manager;
  57. m_dumpAssetsToFile = dumpAssetsToFile;
  58. }
  59. public AssetCapsUploader RequestCapsUploader()
  60. {
  61. AssetCapsUploader uploader = new AssetCapsUploader();
  62. CapsUploaders.Add(uploader);
  63. return uploader;
  64. }
  65. public NoteCardCapsUpdate RequestNoteCardUpdater()
  66. {
  67. NoteCardCapsUpdate update = new NoteCardCapsUpdate();
  68. NotecardUpdaters.Add(update);
  69. return update;
  70. }
  71. public AssetXferUploader RequestXferUploader(LLUUID transactionID)
  72. {
  73. if (!XferUploaders.ContainsKey(transactionID))
  74. {
  75. AssetXferUploader uploader = new AssetXferUploader(this, m_dumpAssetsToFile);
  76. lock (XferUploaders)
  77. {
  78. XferUploaders.Add(transactionID, uploader);
  79. }
  80. return uploader;
  81. }
  82. return null;
  83. }
  84. public void HandleXfer(ulong xferID, uint packetID, byte[] data)
  85. {
  86. AssetXferUploader uploaderFound = null;
  87. lock (XferUploaders)
  88. {
  89. foreach (AssetXferUploader uploader in XferUploaders.Values)
  90. {
  91. if (uploader.XferID == xferID)
  92. {
  93. if (uploader.HandleXferPacket(xferID, packetID, data))
  94. {
  95. uploaderFound = uploader;
  96. }
  97. break;
  98. }
  99. }
  100. // Remove the uploader once the uploader is complete
  101. //[don't think we can be sure a upload has finished from here, uploads are multi part things]
  102. // [or maybe we can if we do more checking like data lenght checks]
  103. if (uploaderFound != null)
  104. {
  105. // m_log.InfoFormat(
  106. // "[ASSET TRANSACTIONS] Removing asset xfer uploader with transfer id {0}, transaction {1}",
  107. // xferID, uploaderFound.TransactionID);
  108. // XferUploaders.Remove(uploaderFound.TransactionID);
  109. //m_log.InfoFormat("[ASSET TRANSACTIONS] Current uploaders: {0}", XferUploaders.Count);
  110. }
  111. }
  112. }
  113. public void RequestCreateInventoryItem(IClientAPI remoteClient, LLUUID transactionID, LLUUID folderID,
  114. uint callbackID, string description, string name, sbyte invType,
  115. sbyte type, byte wearableType, uint nextOwnerMask)
  116. {
  117. if (XferUploaders.ContainsKey(transactionID))
  118. {
  119. XferUploaders[transactionID].RequestCreateInventoryItem(remoteClient, transactionID, folderID,
  120. callbackID, description, name, invType, type,
  121. wearableType, nextOwnerMask);
  122. }
  123. }
  124. public void RequestUpdateInventoryItem(IClientAPI remoteClient, LLUUID transactionID,
  125. InventoryItemBase item)
  126. {
  127. if (XferUploaders.ContainsKey(transactionID))
  128. {
  129. XferUploaders[transactionID].RequestUpdateInventoryItem(remoteClient, transactionID, item);
  130. }
  131. }
  132. /// <summary>
  133. /// Get an uploaded asset. If the data is successfully retrieved, the transaction will be removed.
  134. /// </summary>
  135. /// <param name="transactionID"></param>
  136. /// <returns>The asset if the upload has completed, null if it has not.</returns>
  137. public AssetBase GetTransactionAsset(LLUUID transactionID)
  138. {
  139. if (XferUploaders.ContainsKey(transactionID))
  140. {
  141. AssetXferUploader uploader = XferUploaders[transactionID];
  142. AssetBase asset = uploader.GetAssetData();
  143. lock (XferUploaders)
  144. {
  145. XferUploaders.Remove(transactionID);
  146. }
  147. return asset;
  148. }
  149. return null;
  150. }
  151. // Nested Types
  152. public class AssetXferUploader
  153. {
  154. // Fields
  155. public bool AddToInventory;
  156. public AssetBase Asset;
  157. public LLUUID InventFolder = LLUUID.Zero;
  158. private IClientAPI ourClient;
  159. public LLUUID TransactionID = LLUUID.Zero;
  160. public bool UploadComplete;
  161. public ulong XferID;
  162. private string m_name = String.Empty;
  163. private string m_description = String.Empty;
  164. private sbyte type = 0;
  165. private sbyte invType = 0;
  166. private uint nextPerm = 0;
  167. private bool m_finished = false;
  168. private bool m_createItem = false;
  169. private AgentAssetTransactions m_userTransactions;
  170. private bool m_storeLocal;
  171. private bool m_dumpAssetToFile;
  172. public AssetXferUploader(AgentAssetTransactions transactions, bool dumpAssetToFile)
  173. {
  174. m_userTransactions = transactions;
  175. m_dumpAssetToFile = dumpAssetToFile;
  176. }
  177. /// <summary>
  178. /// Process transfer data received from the client.
  179. /// </summary>
  180. /// <param name="xferID"></param>
  181. /// <param name="packetID"></param>
  182. /// <param name="data"></param>
  183. /// <returns>True if the transfer is complete, false otherwise or if the xferID was not valid</returns>
  184. public bool HandleXferPacket(ulong xferID, uint packetID, byte[] data)
  185. {
  186. if (XferID == xferID)
  187. {
  188. if (Asset.Data.Length > 1)
  189. {
  190. byte[] destinationArray = new byte[Asset.Data.Length + data.Length];
  191. Array.Copy(Asset.Data, 0, destinationArray, 0, Asset.Data.Length);
  192. Array.Copy(data, 0, destinationArray, Asset.Data.Length, data.Length);
  193. Asset.Data = destinationArray;
  194. }
  195. else
  196. {
  197. byte[] buffer2 = new byte[data.Length - 4];
  198. Array.Copy(data, 4, buffer2, 0, data.Length - 4);
  199. Asset.Data = buffer2;
  200. }
  201. ConfirmXferPacketPacket newPack = new ConfirmXferPacketPacket();
  202. newPack.XferID.ID = xferID;
  203. newPack.XferID.Packet = packetID;
  204. ourClient.OutPacket(newPack, ThrottleOutPacketType.Asset);
  205. if ((packetID & 0x80000000) != 0)
  206. {
  207. SendCompleteMessage();
  208. return true;
  209. }
  210. }
  211. return false;
  212. }
  213. /// <summary>
  214. /// Initialise asset transfer from the client
  215. /// </summary>
  216. /// <param name="xferID"></param>
  217. /// <param name="packetID"></param>
  218. /// <param name="data"></param>
  219. /// <returns>True if the transfer is complete, false otherwise</returns>
  220. public bool Initialise(IClientAPI remoteClient, LLUUID assetID, LLUUID transaction, sbyte type, byte[] data,
  221. bool storeLocal, bool tempFile)
  222. {
  223. ourClient = remoteClient;
  224. Asset = new AssetBase();
  225. Asset.FullID = assetID;
  226. Asset.InvType = type;
  227. Asset.Type = type;
  228. Asset.Data = data;
  229. Asset.Name = "blank";
  230. Asset.Description = "empty";
  231. Asset.Local = storeLocal;
  232. Asset.Temporary = tempFile;
  233. TransactionID = transaction;
  234. m_storeLocal = storeLocal;
  235. if (Asset.Data.Length > 2)
  236. {
  237. SendCompleteMessage();
  238. return true;
  239. }
  240. else
  241. {
  242. RequestStartXfer();
  243. }
  244. return false;
  245. }
  246. protected void RequestStartXfer()
  247. {
  248. UploadComplete = false;
  249. XferID = Util.GetNextXferID();
  250. RequestXferPacket newPack = new RequestXferPacket();
  251. newPack.XferID.ID = XferID;
  252. newPack.XferID.VFileType = Asset.Type;
  253. newPack.XferID.VFileID = Asset.FullID;
  254. newPack.XferID.FilePath = 0;
  255. newPack.XferID.Filename = new byte[0];
  256. ourClient.OutPacket(newPack, ThrottleOutPacketType.Asset);
  257. }
  258. protected void SendCompleteMessage()
  259. {
  260. UploadComplete = true;
  261. AssetUploadCompletePacket newPack = new AssetUploadCompletePacket();
  262. newPack.AssetBlock.Type = Asset.Type;
  263. newPack.AssetBlock.Success = true;
  264. newPack.AssetBlock.UUID = Asset.FullID;
  265. ourClient.OutPacket(newPack, ThrottleOutPacketType.Asset);
  266. m_finished = true;
  267. if (m_createItem)
  268. {
  269. DoCreateItem();
  270. }
  271. else if (m_storeLocal)
  272. {
  273. m_userTransactions.Manager.CommsManager.AssetCache.AddAsset(Asset);
  274. }
  275. // Console.WriteLine("upload complete "+ this.TransactionID);
  276. if (m_dumpAssetToFile)
  277. {
  278. DateTime now = DateTime.Now;
  279. string filename =
  280. String.Format("{6}_{7}_{0:d2}{1:d2}{2:d2}_{3:d2}{4:d2}{5:d2}.dat", now.Year, now.Month, now.Day,
  281. now.Hour, now.Minute, now.Second, Asset.Name, Asset.Type);
  282. SaveAssetToFile(filename, Asset.Data);
  283. }
  284. }
  285. ///Left this in and commented in case there are unforseen issues
  286. //private void SaveAssetToFile(string filename, byte[] data)
  287. //{
  288. // FileStream fs = File.Create(filename);
  289. // BinaryWriter bw = new BinaryWriter(fs);
  290. // bw.Write(data);
  291. // bw.Close();
  292. // fs.Close();
  293. //}
  294. private void SaveAssetToFile(string filename, byte[] data)
  295. {
  296. string assetPath = "UserAssets";
  297. if (!Directory.Exists(assetPath))
  298. {
  299. Directory.CreateDirectory(assetPath);
  300. }
  301. FileStream fs = File.Create(Path.Combine(assetPath, filename));
  302. BinaryWriter bw = new BinaryWriter(fs);
  303. bw.Write(data);
  304. bw.Close();
  305. fs.Close();
  306. }
  307. public void RequestCreateInventoryItem(IClientAPI remoteClient, LLUUID transactionID, LLUUID folderID,
  308. uint callbackID, string description, string name, sbyte invType,
  309. sbyte type, byte wearableType, uint nextOwnerMask)
  310. {
  311. if (TransactionID == transactionID)
  312. {
  313. InventFolder = folderID;
  314. m_name = name;
  315. m_description = description;
  316. this.type = type;
  317. this.invType = invType;
  318. nextPerm = nextOwnerMask;
  319. Asset.Name = name;
  320. Asset.Description = description;
  321. Asset.Type = type;
  322. Asset.InvType = invType;
  323. m_createItem = true;
  324. if (m_finished)
  325. {
  326. DoCreateItem();
  327. }
  328. }
  329. }
  330. public void RequestUpdateInventoryItem(IClientAPI remoteClient, LLUUID transactionID,
  331. InventoryItemBase item)
  332. {
  333. if (TransactionID == transactionID)
  334. {
  335. CachedUserInfo userInfo =
  336. m_userTransactions.Manager.CommsManager.UserProfileCacheService.GetUserDetails(
  337. remoteClient.AgentId);
  338. if (userInfo != null)
  339. {
  340. LLUUID assetID = LLUUID.Combine(transactionID, remoteClient.SecureSessionId);
  341. AssetBase asset
  342. = m_userTransactions.Manager.CommsManager.AssetCache.GetAsset(
  343. assetID, (item.assetType == (int) AssetType.Texture ? true : false));
  344. if (asset == null)
  345. {
  346. asset = m_userTransactions.GetTransactionAsset(transactionID);
  347. }
  348. if (asset != null && asset.FullID == assetID)
  349. {
  350. asset.Name = item.inventoryName;
  351. asset.Description = item.inventoryDescription;
  352. asset.InvType = (sbyte) item.invType;
  353. asset.Type = (sbyte) item.assetType;
  354. item.assetID = asset.FullID;
  355. m_userTransactions.Manager.CommsManager.AssetCache.AddAsset(Asset);
  356. }
  357. userInfo.UpdateItem(remoteClient.AgentId, item);
  358. }
  359. }
  360. }
  361. private void DoCreateItem()
  362. {
  363. //really need to fix this call, if lbsa71 saw this he would die.
  364. m_userTransactions.Manager.CommsManager.AssetCache.AddAsset(Asset);
  365. CachedUserInfo userInfo =
  366. m_userTransactions.Manager.CommsManager.UserProfileCacheService.GetUserDetails(ourClient.AgentId);
  367. if (userInfo != null)
  368. {
  369. InventoryItemBase item = new InventoryItemBase();
  370. item.avatarID = ourClient.AgentId;
  371. item.creatorsID = ourClient.AgentId;
  372. item.inventoryID = LLUUID.Random();
  373. item.assetID = Asset.FullID;
  374. item.inventoryDescription = m_description;
  375. item.inventoryName = m_name;
  376. item.assetType = type;
  377. item.invType = invType;
  378. item.parentFolderID = InventFolder;
  379. item.inventoryBasePermissions = 2147483647;
  380. item.inventoryCurrentPermissions = 2147483647;
  381. item.inventoryNextPermissions = nextPerm;
  382. userInfo.AddItem(ourClient.AgentId, item);
  383. ourClient.SendInventoryItemCreateUpdate(item);
  384. }
  385. }
  386. public AssetBase GetAssetData()
  387. {
  388. if (m_finished)
  389. {
  390. return Asset;
  391. }
  392. return null;
  393. }
  394. }
  395. #region Nested Classes currently not in use (waiting for them to be enabled)
  396. public class AssetCapsUploader
  397. {
  398. // Fields
  399. private BaseHttpServer httpListener;
  400. private LLUUID inventoryItemID;
  401. private string m_assetDescription = String.Empty;
  402. private string m_assetName = String.Empty;
  403. private LLUUID m_folderID;
  404. private LLUUID newAssetID;
  405. private bool m_dumpImageToFile;
  406. private string uploaderPath = String.Empty;
  407. // Events
  408. public event UpLoadedAsset OnUpLoad;
  409. // Methods
  410. public void Initialise(string assetName, string assetDescription, LLUUID assetID, LLUUID inventoryItem,
  411. LLUUID folderID, string path, BaseHttpServer httpServer, bool dumpImageToFile)
  412. {
  413. m_assetName = assetName;
  414. m_assetDescription = assetDescription;
  415. m_folderID = folderID;
  416. newAssetID = assetID;
  417. inventoryItemID = inventoryItem;
  418. uploaderPath = path;
  419. httpListener = httpServer;
  420. m_dumpImageToFile = dumpImageToFile;
  421. }
  422. private void SaveImageToFile(string filename, byte[] data)
  423. {
  424. FileStream output = File.Create(filename);
  425. BinaryWriter writer = new BinaryWriter(output);
  426. writer.Write(data);
  427. writer.Close();
  428. output.Close();
  429. }
  430. public string uploaderCaps(byte[] data, string path, string param)
  431. {
  432. LLUUID inventoryItemID = this.inventoryItemID;
  433. string text = String.Empty;
  434. LLSDAssetUploadComplete complete = new LLSDAssetUploadComplete();
  435. complete.new_asset = newAssetID.ToString();
  436. complete.new_inventory_item = inventoryItemID;
  437. complete.state = "complete";
  438. text = LLSDHelpers.SerialiseLLSDReply(complete);
  439. httpListener.RemoveStreamHandler("POST", uploaderPath);
  440. if (m_dumpImageToFile)
  441. {
  442. SaveImageToFile(m_assetName + ".jp2", data);
  443. }
  444. if (OnUpLoad != null)
  445. {
  446. OnUpLoad(m_assetName, "description", newAssetID, inventoryItemID, LLUUID.Zero, data, String.Empty, String.Empty);
  447. }
  448. return text;
  449. }
  450. }
  451. public class NoteCardCapsUpdate
  452. {
  453. // Fields
  454. private BaseHttpServer httpListener;
  455. private LLUUID inventoryItemID;
  456. private string m_assetName = String.Empty;
  457. private LLUUID newAssetID;
  458. private bool SaveImages = false;
  459. private string uploaderPath = String.Empty;
  460. // Events
  461. public event UpLoadedAsset OnUpLoad;
  462. // Methods
  463. public void Initialise(LLUUID inventoryItem, string path, BaseHttpServer httpServer)
  464. {
  465. inventoryItemID = inventoryItem;
  466. uploaderPath = path;
  467. httpListener = httpServer;
  468. newAssetID = LLUUID.Random();
  469. }
  470. private void SaveImageToFile(string filename, byte[] data)
  471. {
  472. FileStream output = File.Create(filename);
  473. BinaryWriter writer = new BinaryWriter(output);
  474. writer.Write(data);
  475. writer.Close();
  476. output.Close();
  477. }
  478. public string uploaderCaps(byte[] data, string path, string param)
  479. {
  480. LLUUID inventoryItemID = this.inventoryItemID;
  481. string text = String.Empty;
  482. LLSDAssetUploadComplete complete = new LLSDAssetUploadComplete();
  483. complete.new_asset = newAssetID.ToString();
  484. complete.new_inventory_item = inventoryItemID;
  485. complete.state = "complete";
  486. text = LLSDHelpers.SerialiseLLSDReply(complete);
  487. httpListener.RemoveStreamHandler("POST", uploaderPath);
  488. if (SaveImages)
  489. {
  490. SaveImageToFile(m_assetName + "notecard.txt", data);
  491. }
  492. if (OnUpLoad != null)
  493. {
  494. OnUpLoad(m_assetName, "description", newAssetID, inventoryItemID, LLUUID.Zero, data, String.Empty, String.Empty);
  495. }
  496. return text;
  497. }
  498. }
  499. #endregion
  500. }
  501. }
  502. */