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