AssetXferUploader.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. private AssetBase m_asset;
  41. private UUID InventFolder = UUID.Zero;
  42. private sbyte invType = 0;
  43. private bool m_createItem = false;
  44. private uint m_createItemCallback = 0;
  45. private string m_description = String.Empty;
  46. private bool m_dumpAssetToFile;
  47. private bool m_finished = false;
  48. private string m_name = String.Empty;
  49. private bool m_storeLocal;
  50. private uint nextPerm = 0;
  51. private IClientAPI ourClient;
  52. private UUID TransactionID = UUID.Zero;
  53. private sbyte type = 0;
  54. private byte wearableType = 0;
  55. public ulong XferID;
  56. private Scene m_Scene;
  57. public AssetXferUploader(Scene scene, bool dumpAssetToFile)
  58. {
  59. m_Scene = scene;
  60. m_dumpAssetToFile = dumpAssetToFile;
  61. }
  62. /// <summary>
  63. /// Process transfer data received from the client.
  64. /// </summary>
  65. /// <param name="xferID"></param>
  66. /// <param name="packetID"></param>
  67. /// <param name="data"></param>
  68. /// <returns>True if the transfer is complete, false otherwise or if the xferID was not valid</returns>
  69. public bool HandleXferPacket(ulong xferID, uint packetID, byte[] data)
  70. {
  71. if (XferID == xferID)
  72. {
  73. if (m_asset.Data.Length > 1)
  74. {
  75. byte[] destinationArray = new byte[m_asset.Data.Length + data.Length];
  76. Array.Copy(m_asset.Data, 0, destinationArray, 0, m_asset.Data.Length);
  77. Array.Copy(data, 0, destinationArray, m_asset.Data.Length, data.Length);
  78. m_asset.Data = destinationArray;
  79. }
  80. else
  81. {
  82. byte[] buffer2 = new byte[data.Length - 4];
  83. Array.Copy(data, 4, buffer2, 0, data.Length - 4);
  84. m_asset.Data = buffer2;
  85. }
  86. ourClient.SendConfirmXfer(xferID, packetID);
  87. if ((packetID & 0x80000000) != 0)
  88. {
  89. SendCompleteMessage();
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. /// <summary>
  96. /// Initialise asset transfer from the client
  97. /// </summary>
  98. /// <param name="xferID"></param>
  99. /// <param name="packetID"></param>
  100. /// <param name="data"></param>
  101. /// <returns>True if the transfer is complete, false otherwise</returns>
  102. public bool Initialise(IClientAPI remoteClient, UUID assetID,
  103. UUID transaction, sbyte type, byte[] data, bool storeLocal,
  104. bool tempFile)
  105. {
  106. ourClient = remoteClient;
  107. m_asset = new AssetBase(assetID, "blank", type,
  108. remoteClient.AgentId.ToString());
  109. m_asset.Data = data;
  110. m_asset.Description = "empty";
  111. m_asset.Local = storeLocal;
  112. m_asset.Temporary = tempFile;
  113. TransactionID = transaction;
  114. m_storeLocal = storeLocal;
  115. if (m_asset.Data.Length > 2)
  116. {
  117. SendCompleteMessage();
  118. return true;
  119. }
  120. else
  121. {
  122. RequestStartXfer();
  123. }
  124. return false;
  125. }
  126. protected void RequestStartXfer()
  127. {
  128. XferID = Util.GetNextXferID();
  129. ourClient.SendXferRequest(XferID, m_asset.Type, m_asset.FullID,
  130. 0, new byte[0]);
  131. }
  132. protected void SendCompleteMessage()
  133. {
  134. ourClient.SendAssetUploadCompleteMessage(m_asset.Type, true,
  135. m_asset.FullID);
  136. m_finished = true;
  137. if (m_createItem)
  138. {
  139. DoCreateItem(m_createItemCallback);
  140. }
  141. else if (m_storeLocal)
  142. {
  143. m_Scene.AssetService.Store(m_asset);
  144. }
  145. m_log.DebugFormat(
  146. "[ASSET TRANSACTIONS]: Uploaded asset {0} for transaction {1}",
  147. m_asset.FullID, TransactionID);
  148. if (m_dumpAssetToFile)
  149. {
  150. DateTime now = DateTime.Now;
  151. string filename =
  152. String.Format("{6}_{7}_{0:d2}{1:d2}{2:d2}_{3:d2}{4:d2}{5:d2}.dat",
  153. now.Year, now.Month, now.Day, now.Hour, now.Minute,
  154. now.Second, m_asset.Name, m_asset.Type);
  155. SaveAssetToFile(filename, m_asset.Data);
  156. }
  157. }
  158. private void SaveAssetToFile(string filename, byte[] data)
  159. {
  160. string assetPath = "UserAssets";
  161. if (!Directory.Exists(assetPath))
  162. {
  163. Directory.CreateDirectory(assetPath);
  164. }
  165. FileStream fs = File.Create(Path.Combine(assetPath, filename));
  166. BinaryWriter bw = new BinaryWriter(fs);
  167. bw.Write(data);
  168. bw.Close();
  169. fs.Close();
  170. }
  171. public void RequestCreateInventoryItem(IClientAPI remoteClient,
  172. UUID transactionID, UUID folderID, uint callbackID,
  173. string description, string name, sbyte invType,
  174. sbyte type, byte wearableType, uint nextOwnerMask)
  175. {
  176. if (TransactionID == transactionID)
  177. {
  178. InventFolder = folderID;
  179. m_name = name;
  180. m_description = description;
  181. this.type = type;
  182. this.invType = invType;
  183. this.wearableType = wearableType;
  184. nextPerm = nextOwnerMask;
  185. m_asset.Name = name;
  186. m_asset.Description = description;
  187. m_asset.Type = type;
  188. if (m_finished)
  189. {
  190. DoCreateItem(callbackID);
  191. }
  192. else
  193. {
  194. m_createItem = true; //set flag so the inventory item is created when upload is complete
  195. m_createItemCallback = callbackID;
  196. }
  197. }
  198. }
  199. private void DoCreateItem(uint callbackID)
  200. {
  201. m_Scene.AssetService.Store(m_asset);
  202. InventoryItemBase item = new InventoryItemBase();
  203. item.Owner = ourClient.AgentId;
  204. item.CreatorId = ourClient.AgentId.ToString();
  205. item.ID = UUID.Random();
  206. item.AssetID = m_asset.FullID;
  207. item.Description = m_description;
  208. item.Name = m_name;
  209. item.AssetType = type;
  210. item.InvType = invType;
  211. item.Folder = InventFolder;
  212. item.BasePermissions = 0x7fffffff;
  213. item.CurrentPermissions = 0x7fffffff;
  214. item.GroupPermissions=0;
  215. item.EveryOnePermissions=0;
  216. item.NextPermissions = nextPerm;
  217. item.Flags = (uint) wearableType;
  218. item.CreationDate = Util.UnixTimeSinceEpoch();
  219. if (m_Scene.AddInventoryItem(item))
  220. ourClient.SendInventoryItemCreateUpdate(item, callbackID);
  221. else
  222. ourClient.SendAlertMessage("Unable to create inventory item");
  223. }
  224. /// <summary>
  225. /// Get the asset data uploaded in this transfer.
  226. /// </summary>
  227. /// <returns>null if the asset has not finished uploading</returns>
  228. public AssetBase GetAssetData()
  229. {
  230. if (m_finished)
  231. {
  232. return m_asset;
  233. }
  234. return null;
  235. }
  236. }
  237. }