public AssetBase Get(string id)
        {
            AssetBase asset = null;

            if (m_Cache != null)
            {
                if (!m_Cache.Get(id, out asset))
                {
                    return(null);
                }
            }

            if (asset == null || asset.Data == null || asset.Data.Length == 0)
            {
                string uri = MapServer(id) + "/assets/" + id;
                asset = SynchronousRestObjectRequester.MakeRequest <int, AssetBase>("GET", uri, 0, m_Auth);
                if (m_Cache != null)
                {
                    if (asset != null)
                    {
                        m_Cache.Cache(asset);
                    }
                    else
                    {
                        m_Cache.CacheNegative(id);
                    }
                }
            }
            return(asset);
        }
        public virtual AssetBase Get(string id)
        {
            AssetBase asset = null;

            if (m_Cache != null)
            {
                if (!m_Cache.Get(id, out asset))
                {
                    return(null);
                }
            }

            if (asset == null && m_ServerURI != null)
            {
                string uri = m_ServerURI + "/assets/" + id;

                asset = SynchronousRestObjectRequester.MakeRequest <int, AssetBase>("GET", uri, 0, m_Auth);
                if (m_Cache != null)
                {
                    if (asset != null)
                    {
                        m_Cache.Cache(asset);
                    }
                    else
                    {
                        m_Cache.CacheNegative(id);
                    }
                }
            }
            return(asset);
        }
        public void retryStore(AssetBase asset, int nextRetryLevel)
        {
/* this may be bad, so excluding
 *          if (m_Cache != null && !m_Cache.Check(asset.ID))
 *          {
 *              m_log.WarnFormat("[Assets] Upload giveup asset bc no longer in local cache: {0}",
 *                  asset.ID.ToString();
 *              return; // if no longer in cache, it was deleted or expired
 *          }
 */
            string uri = MapServer(asset.FullID.ToString()) + "/assets/";

            string newID = null;

            try
            {
                newID = SynchronousRestObjectRequester.
                        MakeRequest <AssetBase, string>("POST", uri, asset, 100000, m_Auth);
            }
            catch
            {
                newID = null;
            }

            if (newID == null || newID == String.Empty || newID == stringUUIDZero)
            {
                if (nextRetryLevel >= MAXSENDRETRIESLEN)
                {
                    m_log.WarnFormat("[Assets] Giving up on uploading after {2} retries id: {0} type {1}",
                                     asset.ID.ToString(), asset.Type.ToString(), MAXSENDRETRIESLEN);
                }
                else
                {
                    lock (m_sendRetries)
                    {
                        if (m_sendRetries[nextRetryLevel] == null)
                        {
                            m_sendRetries[nextRetryLevel] = new List <AssetBase>();
                        }
                        List <AssetBase> m_queue = m_sendRetries[nextRetryLevel];
                        m_queue.Add(asset);
                        m_log.WarnFormat("[Assets] Upload failed: {0} type {1} will retry later",
                                         asset.ID.ToString(), asset.Type.ToString());
                    }
                }
            }
            else
            {
                m_log.InfoFormat("[Assets] Upload of {0} succeeded after {1} failed attempts", asset.ID.ToString(), nextRetryLevel.ToString());
                if (newID != asset.ID)
                {
                    asset.ID = newID;
                }
            }

            m_Cache?.Cache(asset);
        }
        public static WearableCacheItem[] FromOSD(OSD pInput, IAssetCache dataCache)
        {
            List <WearableCacheItem> ret = new List <WearableCacheItem>();

            if (pInput.Type == OSDType.Array)
            {
                OSDArray itemarray = (OSDArray)pInput;
                foreach (OSDMap item in itemarray)
                {
                    ret.Add(new WearableCacheItem()
                    {
                        TextureIndex = item["textureindex"].AsUInteger(),
                        CacheId      = item["cacheid"].AsUUID(),
                        TextureID    = item["textureid"].AsUUID()
                    });

                    if (dataCache != null && item.ContainsKey("assetdata"))
                    {
                        AssetBase asset = new AssetBase(item["textureid"].AsUUID(), "BakedTexture", (sbyte)AssetType.Texture, UUID.Zero.ToString());
                        asset.Temporary = true;
                        asset.Data      = item["assetdata"].AsBinary();
                        dataCache.Cache(asset);
                    }
                }
            }
            else if (pInput.Type == OSDType.Map)
            {
                OSDMap item = (OSDMap)pInput;
                ret.Add(new WearableCacheItem()
                {
                    TextureIndex = item["textureindex"].AsUInteger(),
                    CacheId      = item["cacheid"].AsUUID(),
                    TextureID    = item["textureid"].AsUUID()
                });
                if (dataCache != null && item.ContainsKey("assetdata"))
                {
                    string    assetCreator = item["assetcreator"].AsString();
                    string    assetName    = item["assetname"].AsString();
                    AssetBase asset        = new AssetBase(item["textureid"].AsUUID(), assetName, (sbyte)AssetType.Texture, assetCreator);
                    asset.Temporary = true;
                    asset.Data      = item["assetdata"].AsBinary();
                    dataCache.Cache(asset);
                }
            }
            else
            {
                return(new WearableCacheItem[0]);
            }
            return(ret.ToArray());
        }
        /// <summary>
        /// Handle raw uploaded baked texture data.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="path"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public void process(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, byte[] data)
        {
            m_timeout.Stop();
            m_httpListener.RemoveSimpleStreamHandler(m_uploaderPath);
            m_timeout.Dispose();

            if (!httpRequest.RemoteIPEndPoint.Address.Equals(m_remoteAddress))
            {
                httpResponse.StatusCode = (int)HttpStatusCode.Unauthorized;
                return;
            }

            // need to check if data is a baked
            try
            {
                UUID      newAssetID = UUID.Random();
                AssetBase asset      = new AssetBase(newAssetID, "Baked Texture", (sbyte)AssetType.Texture, m_agentID.ToString());
                asset.Data      = data;
                asset.Temporary = true;
                asset.Local     = true;
                //asset.Flags = AssetFlags.AvatarBake;
                m_assetCache.Cache(asset);

                LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete();
                uploadComplete.new_asset          = newAssetID.ToString();
                uploadComplete.new_inventory_item = UUID.Zero;
                uploadComplete.state = "complete";

                httpResponse.RawBuffer  = Util.UTF8NBGetbytes(LLSDHelpers.SerialiseLLSDReply(uploadComplete));
                httpResponse.StatusCode = (int)HttpStatusCode.OK;
                return;
            }
            catch { }
            httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
        }
        public virtual bool UpdateContent(string id, byte[] data)
        {
            if (m_ServerURI == null)
            {
                return(false);
            }

            AssetBase asset = null;

            m_Cache?.Get(id, out asset);


            if (asset == null)
            {
                AssetMetadata metadata = GetMetadata(id);
                if (metadata == null)
                {
                    return(false);
                }

                asset          = new AssetBase(metadata.FullID, metadata.Name, metadata.Type, UUID.Zero.ToString());
                asset.Metadata = metadata;
            }
            asset.Data = data;

            string uri = m_ServerURI + "/assets/" + id;

            if (SynchronousRestObjectRequester.MakeRequest <AssetBase, bool>("POST", uri, asset, m_Auth))
            {
                m_Cache?.Cache(asset, true);
                return(true);
            }
            return(false);
        }
Beispiel #7
0
        private AssetBase CacheMaterialAsAsset(UUID id, UUID agentID, OSDMap mat, SceneObjectPart sop)
        {
            AssetBase asset = null;

            lock (m_Materials)
            {
                if (!m_Materials.ContainsKey(id))
                {
                    m_Materials[id]         = mat;
                    m_MaterialsRefCount[id] = 1;

                    byte[] data = System.Text.Encoding.ASCII.GetBytes(OSDParser.SerializeLLSDXmlString(mat));

                    // This asset might exist already, but it's ok to try to store it again
                    string name = "Material " + ChooseMaterialName(mat, sop);
                    name        = name.Substring(0, Math.Min(64, name.Length)).Trim();
                    asset       = new AssetBase(id, name, (sbyte)OpenSimAssetType.Material, agentID.ToString());
                    asset.Data  = data;
                    asset.Local = true;
                    m_cache.Cache(asset);
                }
                else
                {
                    m_MaterialsRefCount[id]++;
                }
            }
            return(asset);
        }
Beispiel #8
0
        public AssetBase Get(string id)
        {
            //m_log.DebugFormat("[HG ASSET CONNECTOR]: Get {0}", id);
            AssetBase asset = null;

            if (IsHG(id))
            {
                asset = GetForeign(id);
                if (asset != null)
                {
                    // Now store it locally, if allowed
                    if (m_AssetPerms != null && !m_AssetPerms.AllowedImport(asset.Type))
                    {
                        return(null);
                    }
                    Store(asset);
                }
            }
            else
            {
                if (m_Cache != null)
                {
                    if (!m_Cache.Get(id, out asset))
                    {
                        return(null);
                    }
                    if (asset != null)
                    {
                        return(asset);
                    }
                }
                asset = GetFromLocal(id);
                if (m_Cache != null)
                {
                    if (asset == null)
                    {
                        m_Cache.CacheNegative(id);
                    }
                    else
                    {
                        m_Cache.Cache(asset);
                    }
                }
            }
            return(asset);
        }
        public AssetBase Get(string id)
        {
            AssetBase asset = null;

            if (m_Cache != null)
            {
                if (!m_Cache.Get(id, out asset))
                {
                    return(null);
                }
            }

            if (asset == null)
            {
                asset = m_AssetService.Get(id);
                if (m_Cache != null)
                {
                    if (asset != null)
                    {
                        m_Cache.Cache(asset);
                    }
                    else
                    {
                        m_Cache.CacheNegative(id);
                    }
                }

                //if (null == asset)
                //    m_log.WarnFormat("[LOCAL ASSET SERVICES CONNECTOR]: Could not synchronously find asset with id {0}", id);
            }

            return(asset);
        }
Beispiel #10
0
        public AssetBase Get(string id)
        {
//            m_log.DebugFormat("[LOCAL ASSET SERVICES CONNECTOR]: Synchronously requesting asset {0}", id);

            AssetBase asset = null;

            if (m_Cache != null)
            {
                if (!m_Cache.Get(id, out asset))
                {
                    return(null);
                }
            }

            if (asset == null)
            {
                asset = m_AssetService.Get(id);
                if ((m_Cache != null) && (asset != null))
                {
                    m_Cache.Cache(asset);
                }

//                if (null == asset)
//                    m_log.WarnFormat("[LOCAL ASSET SERVICES CONNECTOR]: Could not synchronously find asset with id {0}", id);
            }

            return(asset);
        }
Beispiel #11
0
        public AssetBase Get(string id)
        {
            //m_log.DebugFormat("[HG ASSET CONNECTOR]: Get {0}", id);
            AssetBase asset = null;

            if (m_Cache != null)
            {
                if (!m_Cache.Get(id, out asset))
                {
                    return(null);
                }

                if (asset != null)
                {
                    return(asset);
                }
            }

            if (IsHG(id))
            {
                asset = m_HGService.Get(id);
                if (asset != null)
                {
                    // Now store it locally, if allowed
                    if (m_AssetPerms.AllowedImport(asset.Type))
                    {
                        m_GridService.Store(asset);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            else
            {
                asset = m_GridService.Get(id);
            }

            if (m_Cache != null)
            {
                m_Cache.Cache(asset);
            }

            return(asset);
        }
        /// <summary>
        /// Creates a new asset
        /// </summary>
        /// Returns a random ID if none is passed into it
        /// <param name="asset"></param>
        /// <returns></returns>
        public string Store(AssetBase asset)
        {
            if (String.IsNullOrEmpty(m_serverUrl))
            {
                m_log.Error("[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
                throw new InvalidOperationException();
            }

            bool storedInCache = false;

            // AssetID handling
            if (String.IsNullOrEmpty(asset.ID) || asset.ID == ZeroID)
            {
                asset.FullID = UUID.Random();
                asset.ID     = asset.FullID.ToString();
            }

            // Cache handling
            if (m_cache != null)
            {
                m_cache.Cache(asset);
                storedInCache = true;
            }

            // Local asset handling
            if (asset.Local)
            {
                if (!storedInCache)
                {
                    m_log.Error("Cannot store local " + asset.Metadata.ContentType + " asset without an asset cache");
                    asset.ID     = null;
                    asset.FullID = UUID.Zero;
                }

                return(asset.ID);
            }

            return(SimianStoreOperation(asset));
        }
        public AssetBase Get(string id)
        {
            string uri = MapServer(id) + "/assets/" + id;

            AssetBase asset = null;

            if (m_Cache != null)
            {
                if (!m_Cache.Get(id, out asset))
                {
                    return(null);
                }
            }

            if (asset == null || asset.Data == null || asset.Data.Length == 0)
            {
                // XXX: Commented out for now since this has either never been properly operational or not for some time
                // as m_maxAssetRequestConcurrency was being passed as the timeout, not a concurrency limiting option.
                // Wasn't noticed before because timeout wasn't actually used.
                // Not attempting concurrency setting for now as this omission was discovered in release candidate
                // phase for OpenSimulator 0.8.  Need to revisit afterwards.
//                asset
//                    = SynchronousRestObjectRequester.MakeRequest<int, AssetBase>(
//                        "GET", uri, 0, m_maxAssetRequestConcurrency);

                asset = SynchronousRestObjectRequester.MakeRequest <int, AssetBase>("GET", uri, 0, m_Auth);


                if (m_Cache != null)
                {
                    if (asset != null)
                    {
                        m_Cache.Cache(asset);
                    }
                    else
                    {
                        m_Cache.CacheNegative(id);
                    }
                }
            }
            return(asset);
        }
Beispiel #14
0
        public virtual string Store(AssetBase asset)
        {
            // Have to assign the asset ID here. This isn't likely to
            // trigger since current callers don't pass emtpy IDs
            // We need the asset ID to route the request to the proper
            // cluster member, so we can't have the server assign one.
            if (asset.ID == string.Empty || asset.ID == stringUUIDZero)
            {
                if (asset.FullID == UUID.Zero)
                {
                    asset.FullID = UUID.Random();
                }
                m_log.WarnFormat("[Assets] Zero ID: {0}", asset.Name);
                asset.ID = asset.FullID.ToString();
            }

            if (asset.FullID == UUID.Zero)
            {
                if (UUID.TryParse(asset.ID, out UUID uuid))
                {
                    asset.FullID = uuid;
                }
                else
                {
                    m_log.WarnFormat("[Assets] Zero IDs: {0}", asset.Name);
                    asset.FullID = UUID.Random();
                    asset.ID     = asset.FullID.ToString();
                }
            }

            if (asset.Temporary || asset.Local)
            {
                m_Cache?.Cache(asset);
                return(asset.ID);
            }

            if (m_ServerURI == null)
            {
                return(null);
            }

            string uri = m_ServerURI + "/assets/";

            string newID = null;

            try
            {
                newID = SynchronousRestObjectRequester.MakeRequest <AssetBase, string>("POST", uri, asset, 10000, m_Auth);
            }
            catch
            {
                newID = null;
            }

            if (string.IsNullOrEmpty(newID) || newID == stringUUIDZero)
            {
                return(string.Empty);
            }
            else
            {
                if (newID != asset.ID)
                {
                    // Placing this here, so that this work with old asset servers that don't send any reply back
                    // SynchronousRestObjectRequester returns somethins that is not an empty string
                    asset.ID = newID;
                    m_Cache?.Cache(asset);
                }
            }

            return(asset.ID);
        }
            /// <summary>
            /// Called once new texture data has been received for this updater.
            /// </summary>
            /// <param name="data"></param>
            /// <param name="scene"></param>
            /// <param name="isReuseable">True if the data given is reuseable.</param>
            /// <returns>The asset UUID given to the incoming data.</returns>
            public UUID DataReceived(byte[] data, Scene scene)
            {
                // this are local assets and will not work without cache
                IAssetCache iac = scene.RequestModuleInterface <IAssetCache>();

                if (iac == null)
                {
                    return(UUID.Zero);
                }

                SceneObjectPart part = scene.GetSceneObjectPart(PrimID);

                if (part == null || data == null || data.Length <= 1)
                {
                    string msg = string.Format("DynamicTextureModule: Error preparing image using URL {0}", Url);
                    scene.SimChat(Utils.StringToBytes(msg), ChatTypeEnum.Say,
                                  0, part.ParentGroup.RootPart.AbsolutePosition, part.Name, part.UUID, false);

                    return(UUID.Zero);
                }

                byte[]    assetData = null;
                AssetBase oldAsset  = null;

                if (BlendWithOldTexture)
                {
                    Primitive.TextureEntryFace curFace;
                    if (Face == ALL_SIDES)
                    {
                        curFace = part.Shape.Textures.DefaultTexture;
                    }
                    else
                    {
                        try
                        {
                            curFace = part.Shape.Textures.GetFace((uint)Face);
                        }
                        catch
                        {
                            curFace = null;
                        }
                    }
                    if (curFace != null)
                    {
                        oldAsset = scene.AssetService.Get(curFace.TextureID.ToString());

                        if (oldAsset != null)
                        {
                            assetData = BlendTextures(data, oldAsset.Data, FrontAlpha);
                        }
                    }
                }
                else if (FrontAlpha < 255)
                {
                    assetData = BlendTextures(data, null, FrontAlpha);
                }


                if (assetData == null)
                {
                    assetData = new byte[data.Length];
                    Array.Copy(data, assetData, data.Length);
                }

                // Create a new asset for user
                AssetBase asset = new AssetBase(
                    UUID.Random(), "DynamicImage" + Util.RandomClass.Next(1, 10000), (sbyte)AssetType.Texture,
                    part.OwnerID.ToString());

                asset.Data        = assetData;
                asset.Description = string.Format("URL image : {0}", Url);
                if (asset.Description.Length > 128)
                {
                    asset.Description = asset.Description.Substring(0, 128);
                }
                asset.Local     = true; // dynamic images aren't saved in the assets server
                asset.Temporary = ((Disp & DISP_TEMP) != 0);

                iac.Cache(asset);

                UUID oldID = UpdatePart(part, asset.FullID);

                if (oldID != UUID.Zero && ((Disp & DISP_EXPIRE) != 0))
                {
                    if (oldAsset == null)
                    {
                        oldAsset = scene.AssetService.Get(oldID.ToString());
                    }

                    if (oldAsset != null)
                    {
                        if (oldAsset.Temporary)
                        {
                            iac.Expire(oldID.ToString());
                        }
                    }
                }

                return(asset.FullID);
            }