opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
77 {
78 try
79 {
80 string capsBase = "/CAPS/" + m_HostCapsObj.CapsObjectPath;
81 string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000");
82  
83 BakedTextureUploader uploader =
84 new BakedTextureUploader(capsBase + uploaderPath, m_HostCapsObj.HttpListener);
85 uploader.OnUpLoad += BakedTextureUploaded;
86  
87 m_HostCapsObj.HttpListener.AddStreamHandler(
88 new BinaryStreamHandler(
89 "POST", capsBase + uploaderPath, uploader.uploaderCaps, "UploadBakedTexture", null));
90  
91 string protocol = "http://";
92  
93 if (m_HostCapsObj.SSLCaps)
94 protocol = "https://";
95  
96 string uploaderURL = protocol + m_HostCapsObj.HostName + ":" +
97 m_HostCapsObj.Port.ToString() + capsBase + uploaderPath;
98  
99 LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
100 uploadResponse.uploader = uploaderURL;
101 uploadResponse.state = "upload";
102  
103 return LLSDHelpers.SerialiseLLSDReply(uploadResponse);
104 }
105 catch (Exception e)
106 {
107 m_log.ErrorFormat("[UPLOAD BAKED TEXTURE HANDLER]: {0}{1}", e.Message, e.StackTrace);
108 }
109  
110 return null;
111 }
112  
113 /// <summary>
114 /// Called when a baked texture has been successfully uploaded by a client.
115 /// </summary>
116 /// <param name="assetID"></param>
117 /// <param name="data"></param>
118 private void BakedTextureUploaded(UUID assetID, byte[] data)
119 {
120 // m_log.DebugFormat("[UPLOAD BAKED TEXTURE HANDLER]: Received baked texture {0}", assetID.ToString());
121  
122 AssetBase asset;
123 asset = new AssetBase(assetID, "Baked Texture", (sbyte)AssetType.Texture, m_HostCapsObj.AgentID.ToString());
124 asset.Data = data;
125 asset.Temporary = true;
126 asset.Local = !m_persistBakedTextures; // Local assets aren't persisted, non-local are
127 m_assetService.Store(asset);
128 }
129 }
130  
131 class BakedTextureUploader
132 {
133 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
134  
135 public event Action<UUID, byte[]> OnUpLoad;
136  
137 private string uploaderPath = String.Empty;
138 private UUID newAssetID;
139 private IHttpServer httpListener;
140  
141 public BakedTextureUploader(string path, IHttpServer httpServer)
142 {
143 newAssetID = UUID.Random();
144 uploaderPath = path;
145 httpListener = httpServer;
146 // m_log.InfoFormat("[CAPS] baked texture upload starting for {0}",newAssetID);
147 }
148  
149 /// <summary>
150 /// Handle raw uploaded baked texture data.
151 /// </summary>
152 /// <param name="data"></param>
153 /// <param name="path"></param>
154 /// <param name="param"></param>
155 /// <returns></returns>
156 public string uploaderCaps(byte[] data, string path, string param)
157 {
158 Action<UUID, byte[]> handlerUpLoad = OnUpLoad;
159  
160 // Don't do this asynchronously, otherwise it's possible for the client to send set appearance information
161 // on another thread which might send out avatar updates before the asset has been put into the asset
162 // service.
163 if (handlerUpLoad != null)
164 handlerUpLoad(newAssetID, data);
165  
166 string res = String.Empty;
167 LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete();
168 uploadComplete.new_asset = newAssetID.ToString();
169 uploadComplete.new_inventory_item = UUID.Zero;
170 uploadComplete.state = "complete";
171  
172 res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
173  
174 httpListener.RemoveStreamHandler("POST", uploaderPath);
175  
176 // m_log.DebugFormat("[BAKED TEXTURE UPLOADER]: baked texture upload completed for {0}", newAssetID);
177  
178 return res;
179 }
180 }
181 }