AssetXferUploader.cs 9.8 KB

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