aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules/Agent/AssetTransaction/AssetXferUploader.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Environment/Modules/Agent/AssetTransaction/AssetXferUploader.cs')
-rw-r--r--OpenSim/Region/Environment/Modules/Agent/AssetTransaction/AssetXferUploader.cs311
1 files changed, 311 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/Agent/AssetTransaction/AssetXferUploader.cs b/OpenSim/Region/Environment/Modules/Agent/AssetTransaction/AssetXferUploader.cs
new file mode 100644
index 0000000..5edfe5d
--- /dev/null
+++ b/OpenSim/Region/Environment/Modules/Agent/AssetTransaction/AssetXferUploader.cs
@@ -0,0 +1,311 @@
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
28using System;
29using System.IO;
30using System.Reflection;
31using log4net;
32using OpenMetaverse;
33using OpenMetaverse.Packets;
34using OpenSim.Framework;
35using OpenSim.Framework.Communications.Cache;
36using OpenSim.Region.Environment.Scenes;
37
38namespace OpenSim.Region.Environment.Modules.Agent.AssetTransaction
39{
40 public class AssetXferUploader
41 {
42 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
43
44 public bool AddToInventory;
45 public AssetBase Asset;
46 public UUID InventFolder = UUID.Zero;
47 private sbyte invType = 0;
48 private bool m_createItem = false;
49 private string m_description = String.Empty;
50 private bool m_dumpAssetToFile;
51 private bool m_finished = false;
52 private string m_name = String.Empty;
53 private bool m_storeLocal;
54 private AgentAssetTransactions m_userTransactions;
55 private uint nextPerm = 0;
56 private IClientAPI ourClient;
57 public UUID TransactionID = UUID.Zero;
58 private sbyte type = 0;
59 public bool UploadComplete;
60 private byte wearableType = 0;
61 public ulong XferID;
62
63 public AssetXferUploader(AgentAssetTransactions transactions, bool dumpAssetToFile)
64 {
65 m_userTransactions = transactions;
66 m_dumpAssetToFile = dumpAssetToFile;
67 }
68
69 /// <summary>
70 /// Process transfer data received from the client.
71 /// </summary>
72 /// <param name="xferID"></param>
73 /// <param name="packetID"></param>
74 /// <param name="data"></param>
75 /// <returns>True if the transfer is complete, false otherwise or if the xferID was not valid</returns>
76 public bool HandleXferPacket(ulong xferID, uint packetID, byte[] data)
77 {
78 if (XferID == xferID)
79 {
80 if (Asset.Data.Length > 1)
81 {
82 byte[] destinationArray = new byte[Asset.Data.Length + data.Length];
83 Array.Copy(Asset.Data, 0, destinationArray, 0, Asset.Data.Length);
84 Array.Copy(data, 0, destinationArray, Asset.Data.Length, data.Length);
85 Asset.Data = destinationArray;
86 }
87 else
88 {
89 byte[] buffer2 = new byte[data.Length - 4];
90 Array.Copy(data, 4, buffer2, 0, data.Length - 4);
91 Asset.Data = buffer2;
92 }
93
94 ourClient.SendConfirmXfer(xferID, packetID);
95
96 if ((packetID & 0x80000000) != 0)
97 {
98 SendCompleteMessage();
99 return true;
100 }
101 }
102
103 return false;
104 }
105
106 /// <summary>
107 /// Initialise asset transfer from the client
108 /// </summary>
109 /// <param name="xferID"></param>
110 /// <param name="packetID"></param>
111 /// <param name="data"></param>
112 /// <returns>True if the transfer is complete, false otherwise</returns>
113 public bool Initialise(IClientAPI remoteClient, UUID assetID, UUID transaction, sbyte type, byte[] data,
114 bool storeLocal, bool tempFile)
115 {
116 ourClient = remoteClient;
117 Asset = new AssetBase();
118 Asset.FullID = assetID;
119 Asset.Type = type;
120 Asset.Data = data;
121 Asset.Name = "blank";
122 Asset.Description = "empty";
123 Asset.Local = storeLocal;
124 Asset.Temporary = tempFile;
125
126 TransactionID = transaction;
127 m_storeLocal = storeLocal;
128
129 if (Asset.Data.Length > 2)
130 {
131 SendCompleteMessage();
132 return true;
133 }
134 else
135 {
136 RequestStartXfer();
137 }
138
139 return false;
140 }
141
142 protected void RequestStartXfer()
143 {
144 UploadComplete = false;
145 XferID = Util.GetNextXferID();
146 ourClient.SendXferRequest(XferID, Asset.Type, Asset.FullID, 0, new byte[0]);
147 }
148
149 protected void SendCompleteMessage()
150 {
151 UploadComplete = true;
152
153 ourClient.SendAssetUploadCompleteMessage(Asset.Type, true, Asset.FullID);
154
155 m_finished = true;
156 if (m_createItem)
157 {
158 DoCreateItem();
159 }
160 else if (m_storeLocal)
161 {
162 m_userTransactions.Manager.MyScene.CommsManager.AssetCache.AddAsset(Asset);
163 }
164
165 m_log.DebugFormat("[ASSET TRANSACTIONS]: Uploaded asset data for transaction {0}", TransactionID);
166
167 if (m_dumpAssetToFile)
168 {
169 DateTime now = DateTime.Now;
170 string filename =
171 String.Format("{6}_{7}_{0:d2}{1:d2}{2:d2}_{3:d2}{4:d2}{5:d2}.dat", now.Year, now.Month, now.Day,
172 now.Hour, now.Minute, now.Second, Asset.Name, Asset.Type);
173 SaveAssetToFile(filename, Asset.Data);
174 }
175 }
176
177 private void SaveAssetToFile(string filename, byte[] data)
178 {
179 string assetPath = "UserAssets";
180 if (!Directory.Exists(assetPath))
181 {
182 Directory.CreateDirectory(assetPath);
183 }
184 FileStream fs = File.Create(Path.Combine(assetPath, filename));
185 BinaryWriter bw = new BinaryWriter(fs);
186 bw.Write(data);
187 bw.Close();
188 fs.Close();
189 }
190
191 public void RequestCreateInventoryItem(IClientAPI remoteClient, UUID transactionID, UUID folderID,
192 uint callbackID, string description, string name, sbyte invType,
193 sbyte type, byte wearableType, uint nextOwnerMask)
194 {
195 if (TransactionID == transactionID)
196 {
197 InventFolder = folderID;
198 m_name = name;
199 m_description = description;
200 this.type = type;
201 this.invType = invType;
202 this.wearableType = wearableType;
203 nextPerm = nextOwnerMask;
204 Asset.Name = name;
205 Asset.Description = description;
206 Asset.Type = type;
207 m_createItem = true;
208
209 if (m_finished)
210 {
211 DoCreateItem();
212 }
213 }
214 }
215
216 public void RequestUpdateInventoryItem(IClientAPI remoteClient, UUID transactionID,
217 InventoryItemBase item)
218 {
219 if (TransactionID == transactionID)
220 {
221 CachedUserInfo userInfo =
222 m_userTransactions.Manager.MyScene.CommsManager.UserProfileCacheService.GetUserDetails(
223 remoteClient.AgentId);
224
225 if (userInfo != null)
226 {
227 UUID assetID = UUID.Combine(transactionID, remoteClient.SecureSessionId);
228
229 AssetBase asset
230 = m_userTransactions.Manager.MyScene.CommsManager.AssetCache.GetAsset(
231 assetID, (item.AssetType == (int) AssetType.Texture ? true : false));
232
233 if (asset == null)
234 {
235 asset = m_userTransactions.GetTransactionAsset(transactionID);
236 }
237
238 if (asset != null && asset.FullID == assetID)
239 {
240 // Assets never get updated, new ones get created
241 asset.FullID = UUID.Random();
242 asset.Name = item.Name;
243 asset.Description = item.Description;
244 asset.Type = (sbyte) item.AssetType;
245 item.AssetID = asset.FullID;
246
247 m_userTransactions.Manager.MyScene.CommsManager.AssetCache.AddAsset(Asset);
248 }
249
250 userInfo.UpdateItem(item);
251 }
252 }
253 }
254
255 public void RequestUpdateTaskInventoryItem(
256 IClientAPI remoteClient, SceneObjectPart part, UUID transactionID, TaskInventoryItem item)
257 {
258 m_log.DebugFormat(
259 "[ASSET TRANSACTIONS]: Updating task item {0} in {1} with asset in transaction {2}",
260 item.Name, part.Name, transactionID);
261
262 Asset.Name = item.Name;
263 Asset.Description = item.Description;
264 Asset.Type = (sbyte) item.Type;
265 item.AssetID = Asset.FullID;
266
267 m_userTransactions.Manager.MyScene.CommsManager.AssetCache.AddAsset(Asset);
268
269 if (part.UpdateInventoryItem(item))
270 part.GetProperties(remoteClient);
271 }
272
273 private void DoCreateItem()
274 {
275 //really need to fix this call, if lbsa71 saw this he would die.
276 m_userTransactions.Manager.MyScene.CommsManager.AssetCache.AddAsset(Asset);
277 CachedUserInfo userInfo =
278 m_userTransactions.Manager.MyScene.CommsManager.UserProfileCacheService.GetUserDetails(ourClient.AgentId);
279 if (userInfo != null)
280 {
281 InventoryItemBase item = new InventoryItemBase();
282 item.Owner = ourClient.AgentId;
283 item.Creator = ourClient.AgentId;
284 item.ID = UUID.Random();
285 item.AssetID = Asset.FullID;
286 item.Description = m_description;
287 item.Name = m_name;
288 item.AssetType = type;
289 item.InvType = invType;
290 item.Folder = InventFolder;
291 item.BasePermissions = 0x7fffffff;
292 item.CurrentPermissions = 0x7fffffff;
293 item.EveryOnePermissions=0;
294 item.NextPermissions = nextPerm;
295 item.Flags = (uint) wearableType;
296
297 userInfo.AddItem(item);
298 ourClient.SendInventoryItemCreateUpdate(item);
299 }
300 }
301
302 public AssetBase GetAssetData()
303 {
304 if (m_finished)
305 {
306 return Asset;
307 }
308 return null;
309 }
310 }
311}