diff options
Diffstat (limited to 'OpenSim/Capabilities/Handlers/UploadBakedTexture/UploadBakedTextureHandler.cs')
-rw-r--r-- | OpenSim/Capabilities/Handlers/UploadBakedTexture/UploadBakedTextureHandler.cs | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/OpenSim/Capabilities/Handlers/UploadBakedTexture/UploadBakedTextureHandler.cs b/OpenSim/Capabilities/Handlers/UploadBakedTexture/UploadBakedTextureHandler.cs new file mode 100644 index 0000000..b89fd6a --- /dev/null +++ b/OpenSim/Capabilities/Handlers/UploadBakedTexture/UploadBakedTextureHandler.cs | |||
@@ -0,0 +1,183 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections; | ||
30 | using System.Collections.Specialized; | ||
31 | using System.Drawing; | ||
32 | using System.Drawing.Imaging; | ||
33 | using System.Reflection; | ||
34 | using System.IO; | ||
35 | using System.Web; | ||
36 | using log4net; | ||
37 | using Nini.Config; | ||
38 | using OpenMetaverse; | ||
39 | using OpenMetaverse.StructuredData; | ||
40 | using OpenMetaverse.Imaging; | ||
41 | using OpenSim.Framework; | ||
42 | using OpenSim.Framework.Capabilities; | ||
43 | using OpenSim.Framework.Servers; | ||
44 | using OpenSim.Framework.Servers.HttpServer; | ||
45 | using OpenSim.Region.Framework.Interfaces; | ||
46 | using OpenSim.Services.Interfaces; | ||
47 | using Caps = OpenSim.Framework.Capabilities.Caps; | ||
48 | |||
49 | namespace OpenSim.Capabilities.Handlers | ||
50 | { | ||
51 | public class UploadBakedTextureHandler | ||
52 | { | ||
53 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
54 | |||
55 | private Caps m_HostCapsObj; | ||
56 | private IAssetService m_assetService; | ||
57 | private bool m_persistBakedTextures; | ||
58 | |||
59 | public UploadBakedTextureHandler(Caps caps, IAssetService assetService, bool persistBakedTextures) | ||
60 | { | ||
61 | m_HostCapsObj = caps; | ||
62 | m_assetService = assetService; | ||
63 | m_persistBakedTextures = persistBakedTextures; | ||
64 | } | ||
65 | |||
66 | /// <summary> | ||
67 | /// Handle a request from the client for a Uri to upload a baked texture. | ||
68 | /// </summary> | ||
69 | /// <param name="request"></param> | ||
70 | /// <param name="path"></param> | ||
71 | /// <param name="param"></param> | ||
72 | /// <param name="httpRequest"></param> | ||
73 | /// <param name="httpResponse"></param> | ||
74 | /// <returns>The upload response if the request is successful, null otherwise.</returns> | ||
75 | public string UploadBakedTexture( | ||
76 | string request, string path, string param, OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
77 | { | ||
78 | try | ||
79 | { | ||
80 | // m_log.Debug("[CAPS]: UploadBakedTexture Request in region: " + m_regionName); | ||
81 | |||
82 | string capsBase = "/CAPS/" + m_HostCapsObj.CapsObjectPath; | ||
83 | string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000"); | ||
84 | |||
85 | BakedTextureUploader uploader = | ||
86 | new BakedTextureUploader(capsBase + uploaderPath, m_HostCapsObj.HttpListener); | ||
87 | uploader.OnUpLoad += BakedTextureUploaded; | ||
88 | |||
89 | m_HostCapsObj.HttpListener.AddStreamHandler( | ||
90 | new BinaryStreamHandler("POST", capsBase + uploaderPath, | ||
91 | uploader.uploaderCaps)); | ||
92 | |||
93 | string protocol = "http://"; | ||
94 | |||
95 | if (m_HostCapsObj.SSLCaps) | ||
96 | protocol = "https://"; | ||
97 | |||
98 | string uploaderURL = protocol + m_HostCapsObj.HostName + ":" + | ||
99 | m_HostCapsObj.Port.ToString() + capsBase + uploaderPath; | ||
100 | |||
101 | LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse(); | ||
102 | uploadResponse.uploader = uploaderURL; | ||
103 | uploadResponse.state = "upload"; | ||
104 | |||
105 | return LLSDHelpers.SerialiseLLSDReply(uploadResponse); | ||
106 | } | ||
107 | catch (Exception e) | ||
108 | { | ||
109 | m_log.Error("[UPLOAD BAKED TEXTURE HANDLER]: " + e.ToString()); | ||
110 | } | ||
111 | |||
112 | return null; | ||
113 | } | ||
114 | |||
115 | /// <summary> | ||
116 | /// Called when a baked texture has been successfully uploaded by a client. | ||
117 | /// </summary> | ||
118 | /// <param name="assetID"></param> | ||
119 | /// <param name="data"></param> | ||
120 | private void BakedTextureUploaded(UUID assetID, byte[] data) | ||
121 | { | ||
122 | // m_log.WarnFormat("[CAPS]: Received baked texture {0}", assetID.ToString()); | ||
123 | |||
124 | AssetBase asset; | ||
125 | asset = new AssetBase(assetID, "Baked Texture", (sbyte)AssetType.Texture, m_HostCapsObj.AgentID.ToString()); | ||
126 | asset.Data = data; | ||
127 | asset.Temporary = true; | ||
128 | asset.Local = !m_persistBakedTextures; // Local assets aren't persisted, non-local are | ||
129 | m_assetService.Store(asset); | ||
130 | } | ||
131 | } | ||
132 | |||
133 | class BakedTextureUploader | ||
134 | { | ||
135 | // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
136 | |||
137 | public event Action<UUID, byte[]> OnUpLoad; | ||
138 | |||
139 | private string uploaderPath = String.Empty; | ||
140 | private UUID newAssetID; | ||
141 | private IHttpServer httpListener; | ||
142 | |||
143 | public BakedTextureUploader(string path, IHttpServer httpServer) | ||
144 | { | ||
145 | newAssetID = UUID.Random(); | ||
146 | uploaderPath = path; | ||
147 | httpListener = httpServer; | ||
148 | // m_log.InfoFormat("[CAPS] baked texture upload starting for {0}",newAssetID); | ||
149 | } | ||
150 | |||
151 | /// <summary> | ||
152 | /// Handle raw uploaded baked texture data. | ||
153 | /// </summary> | ||
154 | /// <param name="data"></param> | ||
155 | /// <param name="path"></param> | ||
156 | /// <param name="param"></param> | ||
157 | /// <returns></returns> | ||
158 | public string uploaderCaps(byte[] data, string path, string param) | ||
159 | { | ||
160 | Action<UUID, byte[]> handlerUpLoad = OnUpLoad; | ||
161 | |||
162 | // Don't do this asynchronously, otherwise it's possible for the client to send set appearance information | ||
163 | // on another thread which might send out avatar updates before the asset has been put into the asset | ||
164 | // service. | ||
165 | if (handlerUpLoad != null) | ||
166 | handlerUpLoad(newAssetID, data); | ||
167 | |||
168 | string res = String.Empty; | ||
169 | LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete(); | ||
170 | uploadComplete.new_asset = newAssetID.ToString(); | ||
171 | uploadComplete.new_inventory_item = UUID.Zero; | ||
172 | uploadComplete.state = "complete"; | ||
173 | |||
174 | res = LLSDHelpers.SerialiseLLSDReply(uploadComplete); | ||
175 | |||
176 | httpListener.RemoveStreamHandler("POST", uploaderPath); | ||
177 | |||
178 | // m_log.DebugFormat("[BAKED TEXTURE UPLOADER]: baked texture upload completed for {0}", newAssetID); | ||
179 | |||
180 | return res; | ||
181 | } | ||
182 | } | ||
183 | } \ No newline at end of file | ||