diff options
Diffstat (limited to 'OpenSim/Framework/Communications/Cache/AssetTransactions.cs')
-rw-r--r-- | OpenSim/Framework/Communications/Cache/AssetTransactions.cs | 275 |
1 files changed, 275 insertions, 0 deletions
diff --git a/OpenSim/Framework/Communications/Cache/AssetTransactions.cs b/OpenSim/Framework/Communications/Cache/AssetTransactions.cs new file mode 100644 index 0000000..bb9c069 --- /dev/null +++ b/OpenSim/Framework/Communications/Cache/AssetTransactions.cs | |||
@@ -0,0 +1,275 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://www.openmetaverse.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.Collections; | ||
30 | using System.Collections.Generic; | ||
31 | using System.Text; | ||
32 | using System.IO; | ||
33 | using libsecondlife; | ||
34 | using libsecondlife.Packets; | ||
35 | using OpenSim.Framework.Interfaces; | ||
36 | using OpenSim.Framework.Types; | ||
37 | using OpenSim.Framework.Utilities; | ||
38 | using OpenSim.Region.Capabilities; | ||
39 | using OpenSim.Framework.Servers; | ||
40 | |||
41 | namespace OpenSim.Framework.Communications.Caches | ||
42 | { | ||
43 | public class AgentAssetTransactions | ||
44 | { | ||
45 | // Fields | ||
46 | public List<AssetCapsUploader> CapsUploaders = new List<AssetCapsUploader>(); | ||
47 | public List<NoteCardCapsUpdate> NotecardUpdaters = new List<NoteCardCapsUpdate>(); | ||
48 | public LLUUID UserID; | ||
49 | public Dictionary<LLUUID, AssetXferUploader> XferUploaders = new Dictionary<LLUUID, AssetXferUploader>(); | ||
50 | |||
51 | // Methods | ||
52 | public AgentAssetTransactions(LLUUID agentID) | ||
53 | { | ||
54 | this.UserID = agentID; | ||
55 | } | ||
56 | |||
57 | public AssetCapsUploader RequestCapsUploader() | ||
58 | { | ||
59 | AssetCapsUploader uploader = new AssetCapsUploader(); | ||
60 | this.CapsUploaders.Add(uploader); | ||
61 | return uploader; | ||
62 | } | ||
63 | |||
64 | public NoteCardCapsUpdate RequestNoteCardUpdater() | ||
65 | { | ||
66 | NoteCardCapsUpdate update = new NoteCardCapsUpdate(); | ||
67 | this.NotecardUpdaters.Add(update); | ||
68 | return update; | ||
69 | } | ||
70 | |||
71 | public AssetXferUploader RequestXferUploader(LLUUID transactionID) | ||
72 | { | ||
73 | AssetXferUploader uploader = new AssetXferUploader(); | ||
74 | this.XferUploaders.Add(transactionID, uploader); | ||
75 | return uploader; | ||
76 | } | ||
77 | |||
78 | // Nested Types | ||
79 | public class AssetCapsUploader | ||
80 | { | ||
81 | // Fields | ||
82 | private BaseHttpServer httpListener; | ||
83 | private LLUUID inventoryItemID; | ||
84 | private string m_assetDescription = ""; | ||
85 | private string m_assetName = ""; | ||
86 | private LLUUID m_folderID; | ||
87 | private LLUUID newAssetID; | ||
88 | private bool SaveImages; | ||
89 | private string uploaderPath = ""; | ||
90 | |||
91 | // Events | ||
92 | public event UpLoadedTexture OnUpLoad; | ||
93 | |||
94 | // Methods | ||
95 | public void Initialise(string assetName, string assetDescription, LLUUID assetID, LLUUID inventoryItem, LLUUID folderID, string path, BaseHttpServer httpServer) | ||
96 | { | ||
97 | this.m_assetName = assetName; | ||
98 | this.m_assetDescription = assetDescription; | ||
99 | this.m_folderID = folderID; | ||
100 | this.newAssetID = assetID; | ||
101 | this.inventoryItemID = inventoryItem; | ||
102 | this.uploaderPath = path; | ||
103 | this.httpListener = httpServer; | ||
104 | } | ||
105 | |||
106 | private void SaveImageToFile(string filename, byte[] data) | ||
107 | { | ||
108 | FileStream output = File.Create(filename); | ||
109 | BinaryWriter writer = new BinaryWriter(output); | ||
110 | writer.Write(data); | ||
111 | writer.Close(); | ||
112 | output.Close(); | ||
113 | } | ||
114 | |||
115 | public string uploaderCaps(byte[] data, string path, string param) | ||
116 | { | ||
117 | LLUUID inventoryItemID = this.inventoryItemID; | ||
118 | string text = ""; | ||
119 | LLSDAssetUploadComplete complete = new LLSDAssetUploadComplete(); | ||
120 | complete.new_asset = this.newAssetID.ToStringHyphenated(); | ||
121 | complete.new_inventory_item = inventoryItemID; | ||
122 | complete.state = "complete"; | ||
123 | text = LLSDHelpers.SerialiseLLSDReply(complete); | ||
124 | this.httpListener.RemoveStreamHandler("POST", this.uploaderPath); | ||
125 | if (this.SaveImages) | ||
126 | { | ||
127 | this.SaveImageToFile(this.m_assetName + ".jp2", data); | ||
128 | } | ||
129 | if (this.OnUpLoad != null) | ||
130 | { | ||
131 | this.OnUpLoad(this.m_assetName, this.newAssetID, inventoryItemID, data); | ||
132 | } | ||
133 | return text; | ||
134 | } | ||
135 | } | ||
136 | |||
137 | public class AssetXferUploader | ||
138 | { | ||
139 | // Fields | ||
140 | public bool AddToInventory; | ||
141 | public AssetBase Asset; | ||
142 | public LLUUID InventFolder = LLUUID.Zero; | ||
143 | private IClientAPI ourClient; | ||
144 | public LLUUID TransactionID = LLUUID.Zero; | ||
145 | public bool UploadComplete; | ||
146 | public uint XferID; | ||
147 | |||
148 | // Methods | ||
149 | public void HandleXferPacket(uint xferID, uint packetID, byte[] data) | ||
150 | { | ||
151 | if (this.XferID == xferID) | ||
152 | { | ||
153 | if (this.Asset.Data.Length > 1) | ||
154 | { | ||
155 | byte[] destinationArray = new byte[this.Asset.Data.Length + data.Length]; | ||
156 | Array.Copy(this.Asset.Data, 0, destinationArray, 0, this.Asset.Data.Length); | ||
157 | Array.Copy(data, 0, destinationArray, this.Asset.Data.Length, data.Length); | ||
158 | this.Asset.Data = destinationArray; | ||
159 | } | ||
160 | else | ||
161 | { | ||
162 | byte[] buffer2 = new byte[data.Length - 4]; | ||
163 | Array.Copy(data, 4, buffer2, 0, data.Length - 4); | ||
164 | this.Asset.Data = buffer2; | ||
165 | } | ||
166 | ConfirmXferPacketPacket newPack = new ConfirmXferPacketPacket(); | ||
167 | newPack.XferID.ID = xferID; | ||
168 | newPack.XferID.Packet = packetID; | ||
169 | this.ourClient.OutPacket(newPack); | ||
170 | if ((packetID & 0x80000000) != 0) | ||
171 | { | ||
172 | this.SendCompleteMessage(); | ||
173 | } | ||
174 | } | ||
175 | } | ||
176 | |||
177 | public void Initialise(IClientAPI remoteClient, LLUUID assetID, LLUUID transaction, sbyte type, byte[] data) | ||
178 | { | ||
179 | this.ourClient = remoteClient; | ||
180 | this.Asset = new AssetBase(); | ||
181 | this.Asset.FullID = assetID; | ||
182 | this.Asset.InvType = type; | ||
183 | this.Asset.Type = type; | ||
184 | this.Asset.Data = data; | ||
185 | this.Asset.Name = "blank"; | ||
186 | this.Asset.Description = "empty"; | ||
187 | this.TransactionID = transaction; | ||
188 | if (this.Asset.Data.Length > 2) | ||
189 | { | ||
190 | this.SendCompleteMessage(); | ||
191 | } | ||
192 | else | ||
193 | { | ||
194 | this.ReqestStartXfer(); | ||
195 | } | ||
196 | } | ||
197 | |||
198 | protected void ReqestStartXfer() | ||
199 | { | ||
200 | this.UploadComplete = false; | ||
201 | this.XferID = Util.GetNextXferID(); | ||
202 | RequestXferPacket newPack = new RequestXferPacket(); | ||
203 | newPack.XferID.ID = this.XferID; | ||
204 | newPack.XferID.VFileType = this.Asset.Type; | ||
205 | newPack.XferID.VFileID = this.Asset.FullID; | ||
206 | newPack.XferID.FilePath = 0; | ||
207 | newPack.XferID.Filename = new byte[0]; | ||
208 | this.ourClient.OutPacket(newPack); | ||
209 | } | ||
210 | |||
211 | protected void SendCompleteMessage() | ||
212 | { | ||
213 | this.UploadComplete = true; | ||
214 | AssetUploadCompletePacket newPack = new AssetUploadCompletePacket(); | ||
215 | newPack.AssetBlock.Type = this.Asset.Type; | ||
216 | newPack.AssetBlock.Success = true; | ||
217 | newPack.AssetBlock.UUID = this.Asset.FullID; | ||
218 | this.ourClient.OutPacket(newPack); | ||
219 | } | ||
220 | } | ||
221 | |||
222 | public class NoteCardCapsUpdate | ||
223 | { | ||
224 | // Fields | ||
225 | private BaseHttpServer httpListener; | ||
226 | private LLUUID inventoryItemID; | ||
227 | private string m_assetName = ""; | ||
228 | private LLUUID newAssetID; | ||
229 | private bool SaveImages; | ||
230 | private string uploaderPath = ""; | ||
231 | |||
232 | // Events | ||
233 | public event UpLoadedTexture OnUpLoad; | ||
234 | |||
235 | // Methods | ||
236 | public void Initialise(LLUUID inventoryItem, string path, BaseHttpServer httpServer) | ||
237 | { | ||
238 | this.inventoryItemID = inventoryItem; | ||
239 | this.uploaderPath = path; | ||
240 | this.httpListener = httpServer; | ||
241 | this.newAssetID = LLUUID.Random(); | ||
242 | } | ||
243 | |||
244 | private void SaveImageToFile(string filename, byte[] data) | ||
245 | { | ||
246 | FileStream output = File.Create(filename); | ||
247 | BinaryWriter writer = new BinaryWriter(output); | ||
248 | writer.Write(data); | ||
249 | writer.Close(); | ||
250 | output.Close(); | ||
251 | } | ||
252 | |||
253 | public string uploaderCaps(byte[] data, string path, string param) | ||
254 | { | ||
255 | LLUUID inventoryItemID = this.inventoryItemID; | ||
256 | string text = ""; | ||
257 | LLSDAssetUploadComplete complete = new LLSDAssetUploadComplete(); | ||
258 | complete.new_asset = this.newAssetID.ToStringHyphenated(); | ||
259 | complete.new_inventory_item = inventoryItemID; | ||
260 | complete.state = "complete"; | ||
261 | text = LLSDHelpers.SerialiseLLSDReply(complete); | ||
262 | this.httpListener.RemoveStreamHandler("POST", this.uploaderPath); | ||
263 | if (this.SaveImages) | ||
264 | { | ||
265 | this.SaveImageToFile(this.m_assetName + "notecard.txt", data); | ||
266 | } | ||
267 | if (this.OnUpLoad != null) | ||
268 | { | ||
269 | this.OnUpLoad(this.m_assetName, this.newAssetID, inventoryItemID, data); | ||
270 | } | ||
271 | return text; | ||
272 | } | ||
273 | } | ||
274 | } | ||
275 | } \ No newline at end of file | ||