AssetXferUploader.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.IO;
  29. using System.Reflection;
  30. using log4net;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Framework.Scenes;
  34. using OpenSim.Services.Interfaces;
  35. namespace OpenSim.Region.CoreModules.Agent.AssetTransaction
  36. {
  37. public class AssetXferUploader
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. /// <summary>
  41. /// Reference to the object that holds this uploader. Used to remove ourselves from it's list if we
  42. /// are performing a delayed update.
  43. /// </summary>
  44. AgentAssetTransactions m_transactions;
  45. private AssetBase m_asset;
  46. private UUID InventFolder = UUID.Zero;
  47. private sbyte invType = 0;
  48. private bool m_createItem = false;
  49. private uint m_createItemCallback = 0;
  50. private bool m_updateItem = false;
  51. private InventoryItemBase m_updateItemData;
  52. private string m_description = String.Empty;
  53. private bool m_dumpAssetToFile;
  54. private bool m_finished = false;
  55. private string m_name = String.Empty;
  56. private bool m_storeLocal;
  57. private uint nextPerm = 0;
  58. private IClientAPI ourClient;
  59. private UUID TransactionID = UUID.Zero;
  60. private sbyte type = 0;
  61. private byte wearableType = 0;
  62. public ulong XferID;
  63. private Scene m_Scene;
  64. public AssetXferUploader(AgentAssetTransactions transactions, Scene scene, UUID assetID, bool dumpAssetToFile)
  65. {
  66. m_transactions = transactions;
  67. m_Scene = scene;
  68. m_asset = new AssetBase() { FullID = assetID };
  69. m_dumpAssetToFile = dumpAssetToFile;
  70. }
  71. /// <summary>
  72. /// Process transfer data received from the client.
  73. /// </summary>
  74. /// <param name="xferID"></param>
  75. /// <param name="packetID"></param>
  76. /// <param name="data"></param>
  77. /// <returns>True if the transfer is complete, false otherwise or if the xferID was not valid</returns>
  78. public bool HandleXferPacket(ulong xferID, uint packetID, byte[] data)
  79. {
  80. // m_log.DebugFormat(
  81. // "[ASSET XFER UPLOADER]: Received packet {0} for xfer {1} (data length {2})",
  82. // packetID, xferID, data.Length);
  83. if (XferID == xferID)
  84. {
  85. if (m_asset.Data.Length > 1)
  86. {
  87. byte[] destinationArray = new byte[m_asset.Data.Length + data.Length];
  88. Array.Copy(m_asset.Data, 0, destinationArray, 0, m_asset.Data.Length);
  89. Array.Copy(data, 0, destinationArray, m_asset.Data.Length, data.Length);
  90. m_asset.Data = destinationArray;
  91. }
  92. else
  93. {
  94. byte[] buffer2 = new byte[data.Length - 4];
  95. Array.Copy(data, 4, buffer2, 0, data.Length - 4);
  96. m_asset.Data = buffer2;
  97. }
  98. ourClient.SendConfirmXfer(xferID, packetID);
  99. if ((packetID & 0x80000000) != 0)
  100. {
  101. SendCompleteMessage();
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. /// <summary>
  108. /// Initialise asset transfer from the client
  109. /// </summary>
  110. /// <param name="xferID"></param>
  111. /// <param name="packetID"></param>
  112. /// <param name="data"></param>
  113. public void Initialise(IClientAPI remoteClient, UUID assetID,
  114. UUID transaction, sbyte type, byte[] data, bool storeLocal,
  115. bool tempFile)
  116. {
  117. // m_log.DebugFormat(
  118. // "[ASSET XFER UPLOADER]: Initialised xfer from {0}, asset {1}, transaction {2}, type {3}, storeLocal {4}, tempFile {5}, already received data length {6}",
  119. // remoteClient.Name, assetID, transaction, type, storeLocal, tempFile, data.Length);
  120. ourClient = remoteClient;
  121. m_asset.Name = "blank";
  122. m_asset.Description = "empty";
  123. m_asset.Type = type;
  124. m_asset.CreatorID = remoteClient.AgentId.ToString();
  125. m_asset.Data = data;
  126. m_asset.Local = storeLocal;
  127. m_asset.Temporary = tempFile;
  128. TransactionID = transaction;
  129. m_storeLocal = storeLocal;
  130. if (m_asset.Data.Length > 2)
  131. {
  132. SendCompleteMessage();
  133. }
  134. else
  135. {
  136. RequestStartXfer();
  137. }
  138. }
  139. protected void RequestStartXfer()
  140. {
  141. XferID = Util.GetNextXferID();
  142. // m_log.DebugFormat(
  143. // "[ASSET XFER UPLOADER]: Requesting Xfer of asset {0}, type {1}, transfer id {2} from {3}",
  144. // m_asset.FullID, m_asset.Type, XferID, ourClient.Name);
  145. ourClient.SendXferRequest(XferID, m_asset.Type, m_asset.FullID, 0, new byte[0]);
  146. }
  147. protected void SendCompleteMessage()
  148. {
  149. ourClient.SendAssetUploadCompleteMessage(m_asset.Type, true,
  150. m_asset.FullID);
  151. // We must lock in order to avoid a race with a separate thread dealing with an inventory item or create
  152. // message from other client UDP.
  153. lock (this)
  154. {
  155. m_finished = true;
  156. if (m_createItem)
  157. {
  158. DoCreateItem(m_createItemCallback);
  159. }
  160. else if (m_updateItem)
  161. {
  162. StoreAssetForItemUpdate(m_updateItemData);
  163. // Remove ourselves from the list of transactions if completion was delayed until the transaction
  164. // was complete.
  165. // TODO: Should probably do the same for create item.
  166. m_transactions.RemoveXferUploader(TransactionID);
  167. }
  168. else if (m_storeLocal)
  169. {
  170. m_Scene.AssetService.Store(m_asset);
  171. }
  172. }
  173. m_log.DebugFormat(
  174. "[ASSET XFER UPLOADER]: Uploaded asset {0} for transaction {1}",
  175. m_asset.FullID, TransactionID);
  176. if (m_dumpAssetToFile)
  177. {
  178. DateTime now = DateTime.Now;
  179. string filename =
  180. String.Format("{6}_{7}_{0:d2}{1:d2}{2:d2}_{3:d2}{4:d2}{5:d2}.dat",
  181. now.Year, now.Month, now.Day, now.Hour, now.Minute,
  182. now.Second, m_asset.Name, m_asset.Type);
  183. SaveAssetToFile(filename, m_asset.Data);
  184. }
  185. }
  186. private void SaveAssetToFile(string filename, byte[] data)
  187. {
  188. string assetPath = "UserAssets";
  189. if (!Directory.Exists(assetPath))
  190. {
  191. Directory.CreateDirectory(assetPath);
  192. }
  193. FileStream fs = File.Create(Path.Combine(assetPath, filename));
  194. BinaryWriter bw = new BinaryWriter(fs);
  195. bw.Write(data);
  196. bw.Close();
  197. fs.Close();
  198. }
  199. public void RequestCreateInventoryItem(IClientAPI remoteClient,
  200. UUID transactionID, UUID folderID, uint callbackID,
  201. string description, string name, sbyte invType,
  202. sbyte type, byte wearableType, uint nextOwnerMask)
  203. {
  204. if (TransactionID == transactionID)
  205. {
  206. InventFolder = folderID;
  207. m_name = name;
  208. m_description = description;
  209. this.type = type;
  210. this.invType = invType;
  211. this.wearableType = wearableType;
  212. nextPerm = nextOwnerMask;
  213. m_asset.Name = name;
  214. m_asset.Description = description;
  215. m_asset.Type = type;
  216. // We must lock to avoid a race with a separate thread uploading the asset.
  217. lock (this)
  218. {
  219. if (m_finished)
  220. {
  221. DoCreateItem(callbackID);
  222. }
  223. else
  224. {
  225. m_createItem = true; //set flag so the inventory item is created when upload is complete
  226. m_createItemCallback = callbackID;
  227. }
  228. }
  229. }
  230. }
  231. public void RequestUpdateInventoryItem(IClientAPI remoteClient, UUID transactionID, InventoryItemBase item)
  232. {
  233. // We must lock to avoid a race with a separate thread uploading the asset.
  234. lock (this)
  235. {
  236. m_asset.Name = item.Name;
  237. m_asset.Description = item.Description;
  238. m_asset.Type = (sbyte)item.AssetType;
  239. // We must always store the item at this point even if the asset hasn't finished uploading, in order
  240. // to avoid a race condition when the appearance module retrieves the item to set the asset id in
  241. // the AvatarAppearance structure.
  242. item.AssetID = m_asset.FullID;
  243. m_Scene.InventoryService.UpdateItem(item);
  244. if (m_finished)
  245. {
  246. StoreAssetForItemUpdate(item);
  247. }
  248. else
  249. {
  250. // m_log.DebugFormat(
  251. // "[ASSET XFER UPLOADER]: Holding update inventory item request {0} for {1} pending completion of asset xfer for transaction {2}",
  252. // item.Name, remoteClient.Name, transactionID);
  253. m_updateItem = true;
  254. m_updateItemData = item;
  255. }
  256. }
  257. }
  258. /// <summary>
  259. /// Store the asset for the given item.
  260. /// </summary>
  261. /// <param name="item"></param>
  262. private void StoreAssetForItemUpdate(InventoryItemBase item)
  263. {
  264. // m_log.DebugFormat(
  265. // "[ASSET XFER UPLOADER]: Storing asset {0} for earlier item update for {1} for {2}",
  266. // m_asset.FullID, item.Name, ourClient.Name);
  267. m_Scene.AssetService.Store(m_asset);
  268. }
  269. private void DoCreateItem(uint callbackID)
  270. {
  271. m_Scene.AssetService.Store(m_asset);
  272. InventoryItemBase item = new InventoryItemBase();
  273. item.Owner = ourClient.AgentId;
  274. item.CreatorId = ourClient.AgentId.ToString();
  275. item.ID = UUID.Random();
  276. item.AssetID = m_asset.FullID;
  277. item.Description = m_description;
  278. item.Name = m_name;
  279. item.AssetType = type;
  280. item.InvType = invType;
  281. item.Folder = InventFolder;
  282. item.BasePermissions = 0x7fffffff;
  283. item.CurrentPermissions = 0x7fffffff;
  284. item.GroupPermissions=0;
  285. item.EveryOnePermissions=0;
  286. item.NextPermissions = nextPerm;
  287. item.Flags = (uint) wearableType;
  288. item.CreationDate = Util.UnixTimeSinceEpoch();
  289. if (m_Scene.AddInventoryItem(item))
  290. ourClient.SendInventoryItemCreateUpdate(item, callbackID);
  291. else
  292. ourClient.SendAlertMessage("Unable to create inventory item");
  293. }
  294. /// <summary>
  295. /// Get the asset data uploaded in this transfer.
  296. /// </summary>
  297. /// <returns>null if the asset has not finished uploading</returns>
  298. public AssetBase GetAssetData()
  299. {
  300. if (m_finished)
  301. {
  302. return m_asset;
  303. }
  304. return null;
  305. }
  306. }
  307. }