Пример #1
0
        public bool StoreAsset(Asset asset)
        {
            Debug.Assert(asset.Data != null, "Cannot store an asset without data");
            Debug.Assert(!String.IsNullOrEmpty(asset.ContentType), "Cannot store an asset without a ContentType");

            if (asset.ID == UUID.Zero)
            {
                asset.ID = UUID.Random();
            }

            // Run this asset through the incoming asset filter
            if (!m_simian.FilterAsset(asset))
            {
                m_log.InfoFormat("Asset {0} ({1}, {2} bytes) was rejected", asset.ID, asset.ContentType, asset.Data.Length);
                return(false);
            }

            byte[] metadata = CreateMetadata(asset.CreatorID, asset.ContentType, asset.Local, asset.Temporary, asset.Data, asset.ExtraHeaders);

            if (asset.Temporary)
            {
                m_dataStore.AddOrUpdateAsset(asset.ID, METADATA_MIME_TYPE, metadata, true);
                return(m_dataStore.AddOrUpdateAsset(asset.ID, asset.ContentType, asset.Data, true));
            }
            else
            {
                m_dataStore.AddOrUpdateAsset(asset.ID, METADATA_MIME_TYPE, metadata);
                return(m_dataStore.AddOrUpdateAsset(asset.ID, asset.ContentType, asset.Data));
            }
        }
Пример #2
0
        public bool StoreAsset(Asset asset)
        {
            Debug.Assert(asset.Data != null, "Cannot store an asset without data");
            Debug.Assert(!String.IsNullOrEmpty(asset.ContentType), "Cannot store an asset without a ContentType");

            bool storedInCache = false;

            if (asset.ID == UUID.Zero)
            {
                asset.ID = UUID.Random();
            }

            // Run this asset through the incoming asset filter
            if (!m_simian.FilterAsset(asset))
            {
                m_log.InfoFormat("Asset {0} ({1}, {2} bytes) was rejected", asset.ID, asset.ContentType, asset.Data.Length);
                return(false);
            }

            #region Caching

            if (m_dataStore != null)
            {
                byte[] metadata = CreateMetadata(asset.CreatorID, asset.ContentType, asset.Local, asset.Temporary, asset.Data, asset.ExtraHeaders);
                m_dataStore.AddOrUpdateAsset(asset.ID, METADATA_MIME_TYPE, metadata, true);
                m_dataStore.AddOrUpdateAsset(asset.ID, asset.ContentType, asset.Data, true);

                storedInCache = true;
            }

            #endregion Caching

            // If this is a local asset we don't need to store it remotely
            if (asset.Local)
            {
                if (!storedInCache)
                {
                    m_log.Error("Cannot store asset " + asset.ID + " (" + asset.ContentType + ") without an IDataStore");
                }
                return(storedInCache);
            }

            #region Remote Storage

            // Distinguish public and private assets
            bool isPublic = true;
            switch (asset.ContentType)
            {
            case "application/vnd.ll.callingcard":
            case "application/vnd.ll.gesture":
            case "application/vnd.ll.lslbyte":
            case "application/vnd.ll.lsltext":
                isPublic = false;
                break;
            }

            // Build the remote storage request
            List <MultipartForm.Element> postParameters = new List <MultipartForm.Element>()
            {
                new MultipartForm.Parameter("AssetID", asset.ID.ToString()),
                new MultipartForm.Parameter("CreatorID", asset.CreatorID.ToString()),
                new MultipartForm.Parameter("Temporary", asset.Temporary ? "1" : "0"),
                new MultipartForm.Parameter("Public", isPublic ? "1" : "0"),
                new MultipartForm.File("Asset", asset.ID.ToString(), asset.ContentType, asset.Data)
            };

            // Make the remote storage request
            string errorMessage = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(m_serverUrl);

                HttpWebResponse response = MultipartForm.Post(request, postParameters);
                using (Stream responseStream = response.GetResponseStream())
                {
                    string responseStr = null;

                    try
                    {
                        responseStr = responseStream.GetStreamString();
                        OSD responseOSD = OSDParser.Deserialize(responseStr);
                        if (responseOSD.Type == OSDType.Map)
                        {
                            OSDMap responseMap = (OSDMap)responseOSD;
                            if (responseMap["Success"].AsBoolean())
                            {
                                return(true);
                            }
                            else
                            {
                                errorMessage = "Upload failed: " + responseMap["Message"].AsString();
                            }
                        }
                        else
                        {
                            errorMessage = "Response format was invalid:\n" + responseStr;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!String.IsNullOrEmpty(responseStr))
                        {
                            errorMessage = "Failed to parse the response:\n" + responseStr;
                        }
                        else
                        {
                            errorMessage = "Failed to retrieve the response: " + ex.Message;
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                errorMessage = ex.Message;
            }

            #endregion Remote Storage

            m_log.WarnFormat("Failed to remotely store asset {0} ({1}): {2}", asset.ID, asset.ContentType, errorMessage);
            return(false);
        }