Annotation of /trunk/ModularRex/RexNetwork/CapsUpload.cs
Parent Directory
|
Revision Log
Revision 124 - (view) (download)
| 1 : | mikkopa | 107 | using System; |
| 2 : | using System.Collections.Generic; | ||
| 3 : | using System.Text; | ||
| 4 : | using OpenSim.Region.Framework.Interfaces; | ||
| 5 : | using OpenSim.Region.Framework.Scenes; | ||
| 6 : | using OpenSim.Framework; | ||
| 7 : | using OpenSim.Framework.Communications.Capabilities; | ||
| 8 : | using OpenSim.Framework.Servers; | ||
| 9 : | using OpenSim.Framework.Servers.Interfaces; | ||
| 10 : | using OpenMetaverse; | ||
| 11 : | using Caps = OpenSim.Framework.Communications.Capabilities.Caps; | ||
| 12 : | using log4net; | ||
| 13 : | using System.Reflection; | ||
| 14 : | |||
| 15 : | namespace ModularRex.RexNetwork | ||
| 16 : | { | ||
| 17 : | public class CapsUpload : IRegionModule | ||
| 18 : | { | ||
| 19 : | private Scene m_scene; | ||
| 20 : | private ICapabilitiesModule m_capsmodule; | ||
| 21 : | protected Dictionary<UUID, RexCaps> m_capsHandlers = new Dictionary<UUID, RexCaps>(); | ||
| 22 : | |||
| 23 : | #region IRegionModule Members | ||
| 24 : | |||
| 25 : | public void Close() | ||
| 26 : | { | ||
| 27 : | m_scene.EventManager.OnNewClient -= OnNewClient; | ||
| 28 : | m_scene.EventManager.OnRegisterCaps -= OnClientRegisterCaps; | ||
| 29 : | } | ||
| 30 : | |||
| 31 : | public void Initialise(Scene scene, Nini.Config.IConfigSource source) | ||
| 32 : | { | ||
| 33 : | m_scene = scene; | ||
| 34 : | } | ||
| 35 : | |||
| 36 : | public bool IsSharedModule | ||
| 37 : | { | ||
| 38 : | get { return false; } | ||
| 39 : | } | ||
| 40 : | |||
| 41 : | public string Name | ||
| 42 : | { | ||
| 43 : | get { return "rex uploader module"; } | ||
| 44 : | } | ||
| 45 : | |||
| 46 : | public void PostInitialise() | ||
| 47 : | { | ||
| 48 : | m_capsmodule = m_scene.RequestModuleInterface<ICapabilitiesModule>(); | ||
| 49 : | m_scene.EventManager.OnNewClient += OnNewClient; | ||
| 50 : | m_scene.EventManager.OnRegisterCaps += OnClientRegisterCaps; | ||
| 51 : | } | ||
| 52 : | |||
| 53 : | #endregion | ||
| 54 : | |||
| 55 : | private void OnNewClient(IClientAPI client) | ||
| 56 : | { | ||
| 57 : | Caps clientCaps = m_capsmodule.GetCapsHandlerForUser(client.AgentId); | ||
| 58 : | } | ||
| 59 : | |||
| 60 : | private void OnClientRegisterCaps(OpenMetaverse.UUID agentID, Caps caps) | ||
| 61 : | { | ||
| 62 : | RexCaps rexcaps = new RexCaps( | ||
| 63 : | m_scene.CommsManager.AssetCache, | ||
| 64 : | m_scene.CommsManager.HttpServer, | ||
| 65 : | m_scene.RegionInfo.ExternalHostName, | ||
| 66 : | m_scene.CommsManager.HttpServer.Port, | ||
| 67 : | m_scene.DumpAssetsToFile); | ||
| 68 : | rexcaps.UUID = agentID; | ||
| 69 : | rexcaps.Caps = caps; | ||
| 70 : | mikkopa | 124 | rexcaps.GetClient = m_scene.SceneContents.GetControllingClient; |
| 71 : | mikkopa | 107 | |
| 72 : | rexcaps.OverloadHandlers(); | ||
| 73 : | |||
| 74 : | m_capsHandlers[agentID] = rexcaps; | ||
| 75 : | } | ||
| 76 : | } | ||
| 77 : | |||
| 78 : | public class RexCaps | ||
| 79 : | { | ||
| 80 : | private static readonly string m_newInventory = "0002/"; | ||
| 81 : | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
| 82 : | |||
| 83 : | private IHttpServer m_httpListener; | ||
| 84 : | private string m_httpListenerHostName; | ||
| 85 : | private uint m_httpListenPort; | ||
| 86 : | private bool m_dumpAssetsToFile; | ||
| 87 : | private IAssetCache m_assetCache; | ||
| 88 : | private UUID m_agentID; | ||
| 89 : | |||
| 90 : | mikkopa | 124 | public GetClientDelegate GetClient = null; |
| 91 : | |||
| 92 : | mikkopa | 107 | public RexCaps(IAssetCache assetCache, IHttpServer httpServer, string httpListen, uint httpPort, bool dumbAssetsToFile) |
| 93 : | { | ||
| 94 : | m_assetCache = assetCache; | ||
| 95 : | m_httpListener = httpServer; | ||
| 96 : | m_httpListenerHostName = httpListen; | ||
| 97 : | m_httpListenPort = httpPort; | ||
| 98 : | m_dumpAssetsToFile = dumbAssetsToFile; | ||
| 99 : | } | ||
| 100 : | |||
| 101 : | public Caps Caps; | ||
| 102 : | public UUID UUID | ||
| 103 : | { | ||
| 104 : | get { return m_agentID; } | ||
| 105 : | set { m_agentID = value;} | ||
| 106 : | } | ||
| 107 : | |||
| 108 : | public void OverloadHandlers() | ||
| 109 : | { | ||
| 110 : | string capsBase = "/CAPS/" + Caps.CapsObjectPath; | ||
| 111 : | Caps.CapsHandlers["NewFileAgentInventory"] = | ||
| 112 : | new LLSDStreamhandler<LLSDAssetUploadRequest, LLSDAssetUploadResponse>("POST", | ||
| 113 : | capsBase + m_newInventory, | ||
| 114 : | NewAgentInventoryRequest); | ||
| 115 : | } | ||
| 116 : | |||
| 117 : | public LLSDAssetUploadResponse NewAgentInventoryRequest(LLSDAssetUploadRequest llsdRequest) | ||
| 118 : | { | ||
| 119 : | //m_log.Debug("[CAPS]: NewAgentInventoryRequest Request is: " + llsdRequest.ToString()); | ||
| 120 : | //m_log.Debug("asset upload request via CAPS" + llsdRequest.inventory_type + " , " + llsdRequest.asset_type); | ||
| 121 : | |||
| 122 : | mikkopa | 124 | IClientAPI client = null; |
| 123 : | if (GetClient != null) | ||
| 124 : | { | ||
| 125 : | client = GetClient(m_agentID); | ||
| 126 : | } | ||
| 127 : | mikkopa | 107 | |
| 128 : | mikkopa | 124 | #region Upload permissions |
| 129 : | if (client != null) | ||
| 130 : | { | ||
| 131 : | IUploadPermissions uploadPermissionModule = client.Scene.RequestModuleInterface<IUploadPermissions>(); | ||
| 132 : | if (uploadPermissionModule != null) | ||
| 133 : | { | ||
| 134 : | if (!uploadPermissionModule.CanUpload(m_agentID)) | ||
| 135 : | { | ||
| 136 : | client.SendAgentAlertMessage("Unable to upload asset. Insufficient permissions.", false); | ||
| 137 : | mikkopa | 107 | |
| 138 : | mikkopa | 124 | LLSDAssetUploadResponse errorResponse = new LLSDAssetUploadResponse(); |
| 139 : | errorResponse.uploader = ""; | ||
| 140 : | errorResponse.state = "error"; | ||
| 141 : | return errorResponse; | ||
| 142 : | } | ||
| 143 : | } | ||
| 144 : | } | ||
| 145 : | #endregion | ||
| 146 : | mikkopa | 107 | |
| 147 : | mikkopa | 124 | #region Fancy money module stuff: |
| 148 : | if (llsdRequest.asset_type == "texture" || | ||
| 149 : | llsdRequest.asset_type == "animation" || | ||
| 150 : | llsdRequest.asset_type == "sound" || | ||
| 151 : | llsdRequest.asset_type == "ogremesh" || | ||
| 152 : | llsdRequest.asset_type == "flashani") | ||
| 153 : | { | ||
| 154 : | IScene scene = null; | ||
| 155 : | if (client != null) | ||
| 156 : | { | ||
| 157 : | scene = client.Scene; | ||
| 158 : | mikkopa | 107 | |
| 159 : | mikkopa | 124 | IMoneyModule mm = scene.RequestModuleInterface<IMoneyModule>(); |
| 160 : | mikkopa | 107 | |
| 161 : | mikkopa | 124 | if (mm != null) |
| 162 : | { | ||
| 163 : | if (!mm.UploadCovered(client)) | ||
| 164 : | { | ||
| 165 : | if (client != null) | ||
| 166 : | client.SendAgentAlertMessage("Unable to upload asset. Insufficient funds.", false); | ||
| 167 : | |||
| 168 : | LLSDAssetUploadResponse errorResponse = new LLSDAssetUploadResponse(); | ||
| 169 : | errorResponse.uploader = ""; | ||
| 170 : | errorResponse.state = "error"; | ||
| 171 : | return errorResponse; | ||
| 172 : | } | ||
| 173 : | } | ||
| 174 : | } | ||
| 175 : | } | ||
| 176 : | #endregion | ||
| 177 : | |||
| 178 : | mikkopa | 107 | string assetName = llsdRequest.name; |
| 179 : | string assetDes = llsdRequest.description; | ||
| 180 : | string capsBase = "/CAPS/" + Caps.CapsObjectPath; | ||
| 181 : | UUID newAsset = UUID.Random(); | ||
| 182 : | UUID newInvItem = UUID.Random(); | ||
| 183 : | UUID parentFolder = llsdRequest.folder_id; | ||
| 184 : | string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000"); | ||
| 185 : | |||
| 186 : | Caps.AssetUploader uploader = | ||
| 187 : | new Caps.AssetUploader(assetName, assetDes, newAsset, newInvItem, parentFolder, llsdRequest.inventory_type, | ||
| 188 : | llsdRequest.asset_type, capsBase + uploaderPath, m_httpListener, m_dumpAssetsToFile); | ||
| 189 : | m_httpListener.AddStreamHandler( | ||
| 190 : | new BinaryStreamHandler("POST", capsBase + uploaderPath, uploader.uploaderCaps)); | ||
| 191 : | |||
| 192 : | string protocol = "http://"; | ||
| 193 : | |||
| 194 : | if (m_httpListener.UseSSL) | ||
| 195 : | protocol = "https://"; | ||
| 196 : | |||
| 197 : | string uploaderURL = protocol + m_httpListenerHostName + ":" + m_httpListenPort.ToString() + capsBase + | ||
| 198 : | uploaderPath; | ||
| 199 : | |||
| 200 : | LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse(); | ||
| 201 : | uploadResponse.uploader = uploaderURL; | ||
| 202 : | uploadResponse.state = "upload"; | ||
| 203 : | uploader.OnUpLoad += UploadCompleteHandler; | ||
| 204 : | return uploadResponse; | ||
| 205 : | } | ||
| 206 : | |||
| 207 : | public void UploadCompleteHandler(string assetName, string assetDescription, UUID assetID, | ||
| 208 : | UUID inventoryItem, UUID parentFolder, byte[] data, string inventoryType, | ||
| 209 : | string assetType) | ||
| 210 : | { | ||
| 211 : | sbyte assType = 0; | ||
| 212 : | sbyte inType = 0; | ||
| 213 : | |||
| 214 : | mikkopa | 124 | if (inventoryType == "texture") |
| 215 : | mikkopa | 107 | { |
| 216 : | mikkopa | 124 | } |
| 217 : | else if (inventoryType == "sound") | ||
| 218 : | { | ||
| 219 : | mikkopa | 107 | inType = 1; |
| 220 : | assType = 1; | ||
| 221 : | } | ||
| 222 : | else if (inventoryType == "animation") | ||
| 223 : | { | ||
| 224 : | inType = 19; | ||
| 225 : | assType = 20; | ||
| 226 : | } | ||
| 227 : | else if (inventoryType == "wearable") | ||
| 228 : | { | ||
| 229 : | inType = 18; | ||
| 230 : | switch (assetType) | ||
| 231 : | { | ||
| 232 : | case "bodypart": | ||
| 233 : | assType = 13; | ||
| 234 : | break; | ||
| 235 : | case "clothing": | ||
| 236 : | assType = 5; | ||
| 237 : | break; | ||
| 238 : | } | ||
| 239 : | } | ||
| 240 : | else | ||
| 241 : | { | ||
| 242 : | ParseAssetAndInventoryType(assetType, inventoryType, out assType, out inType); | ||
| 243 : | if (assType == 0 || inType == 0) | ||
| 244 : | { | ||
| 245 : | m_log.WarnFormat("[REXCAPS]: Unknown inventory type {0}. Asset type ", inventoryType, assetType); | ||
| 246 : | } | ||
| 247 : | } | ||
| 248 : | |||
| 249 : | AssetBase asset; | ||
| 250 : | asset = new AssetBase(); | ||
| 251 : | asset.FullID = assetID; | ||
| 252 : | asset.Type = assType; | ||
| 253 : | asset.Name = assetName; | ||
| 254 : | asset.Data = data; | ||
| 255 : | if (Caps.AddNewAsset != null) | ||
| 256 : | Caps.AddNewAsset(asset); | ||
| 257 : | else if (m_assetCache != null) | ||
| 258 : | m_assetCache.AddAsset(asset); | ||
| 259 : | |||
| 260 : | InventoryItemBase item = new InventoryItemBase(); | ||
| 261 : | item.Owner = m_agentID; | ||
| 262 : | mikkopa | 108 | item.CreatorId = m_agentID.ToString(); |
| 263 : | mikkopa | 107 | item.ID = inventoryItem; |
| 264 : | item.AssetID = asset.FullID; | ||
| 265 : | item.Description = assetDescription; | ||
| 266 : | item.Name = assetName; | ||
| 267 : | item.AssetType = assType; | ||
| 268 : | item.InvType = inType; | ||
| 269 : | item.Folder = parentFolder; | ||
| 270 : | item.CurrentPermissions = 2147483647; | ||
| 271 : | item.BasePermissions = 2147483647; | ||
| 272 : | item.EveryOnePermissions = 0; | ||
| 273 : | item.NextPermissions = 2147483647; | ||
| 274 : | item.CreationDate = Util.UnixTimeSinceEpoch(); | ||
| 275 : | |||
| 276 : | if (Caps.AddNewInventoryItem != null) | ||
| 277 : | { | ||
| 278 : | Caps.AddNewInventoryItem(m_agentID, item); | ||
| 279 : | } | ||
| 280 : | } | ||
| 281 : | |||
| 282 : | private void ParseAssetAndInventoryType(string assetType, string inventoryType, out sbyte assType, out sbyte inType) | ||
| 283 : | { | ||
| 284 : | inType = 0; | ||
| 285 : | assType = 0; | ||
| 286 : | |||
| 287 : | if (inventoryType == "sound") | ||
| 288 : | { | ||
| 289 : | inType = 1; | ||
| 290 : | assType = 1; | ||
| 291 : | } | ||
| 292 : | else if (inventoryType == "animation") | ||
| 293 : | { | ||
| 294 : | inType = 19; | ||
| 295 : | assType = 20; | ||
| 296 : | } | ||
| 297 : | |||
| 298 : | if (assetType == "ogremesh") | ||
| 299 : | { | ||
| 300 : | inType = 6; | ||
| 301 : | assType = 43; | ||
| 302 : | } | ||
| 303 : | |||
| 304 : | if (assetType == "ogreskel") | ||
| 305 : | { | ||
| 306 : | inType = 19; | ||
| 307 : | assType = 44; | ||
| 308 : | } | ||
| 309 : | |||
| 310 : | if (assetType == "ogrepart") | ||
| 311 : | { | ||
| 312 : | inType = 41; | ||
| 313 : | assType = 47; | ||
| 314 : | } | ||
| 315 : | |||
| 316 : | if (assetType == "ogremate") | ||
| 317 : | { | ||
| 318 : | inType = 41; | ||
| 319 : | assType = 45; | ||
| 320 : | } | ||
| 321 : | if (assetType == "flashani") | ||
| 322 : | { | ||
| 323 : | inType = 42; | ||
| 324 : | assType = 49; | ||
| 325 : | } | ||
| 326 : | } | ||
| 327 : | } | ||
| 328 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

