Beispiel #1
0
        private UploadResult UpdateResult(UploadResult result, MediaCrushBlob blob)
        {
            string url = URLHelpers.CombineURL(APIURL, blob.Hash);

            if (DirectLink)
            {
                if (blob.Files != null && blob.Files.Length > 0)
                {
                    if (blob.BlobType == "image")
                    {
                        url = blob.Files[0].URL;
                    }
                    else if (blob.BlobType == "video" || blob.BlobType == "audio")
                    {
                        url = URLHelpers.CombineURL(APIURL, blob.Hash + "/direct");
                    }
                }
            }

            result.URL         = url;
            result.DeletionURL = URLHelpers.CombineURL(APIURL, blob.Hash + "/delete");

            return(result);
        }
Beispiel #2
0
        public override UploadResult Upload(Stream stream, string fileName)
        {
            ThrowWebExceptions = true;

            string hash = CreateHash(stream);

            UploadResult result = CheckExists(hash);

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

            try
            {
                result = UploadData(stream, "https://mediacru.sh/api/upload/file", fileName);
            }
            catch (WebException e)
            {
                HttpWebResponse response = e.Response as HttpWebResponse;

                if (response == null)
                {
                    throw;
                }

                if (response.StatusCode == HttpStatusCode.Conflict)
                {
                    return(HandleDuplicate(response));
                }

                throw;
            }

            hash = JToken.Parse(result.Response)["hash"].Value <string>();

            while (!StopUploadRequested)
            {
                result.Response = SendRequest(HttpMethod.GET, "https://mediacru.sh/api/" + hash + "/status");
                JToken jsonResponse = JToken.Parse(result.Response);
                string status       = jsonResponse["status"].Value <string>();

                switch (status)
                {
                case "processing":
                case "pending":
                    Thread.Sleep(500);
                    break;

                case "done":
                case "ready":
                    MediaCrushBlob blob = jsonResponse[hash].ToObject <MediaCrushBlob>();
                    result.URL         = DirectLink ? blob.DirectURL : blob.URL;
                    result.DeletionURL = blob.DeletionURL;
                    return(result);

                case "unrecognized":
                    // Note: MediaCrush accepts just about _every_ kind of media file,
                    // so the file itself is probably corrupted or just not actually a media file
                    throw new Exception("This file is not an acceptable file type.");

                case "timeout":
                    throw new Exception("This file took too long to process.");

                default:
                    throw new Exception("This file failed to process.");
                }
            }

            return(result);
        }
        private UploadResult UpdateResult(UploadResult result, MediaCrushBlob blob)
        {
            string url = URLHelpers.CombineURL(APIURL, blob.Hash);

            if (DirectLink)
            {
                if (blob.Files != null && blob.Files.Length > 0)
                {
                    if (blob.BlobType == "image")
                    {
                        url = blob.Files[0].URL;
                    }
                    else if (blob.BlobType == "video" || blob.BlobType == "audio")
                    {
                        url = URLHelpers.CombineURL(APIURL, blob.Hash + "/direct");
                    }
                }
            }

            result.URL = url;
            result.DeletionURL = URLHelpers.CombineURL(APIURL, blob.Hash + "/delete");

            return result;
        }