Exemple #1
0
        private UploadResult CheckExists(string hash)
        {
            try
            {
                string response = SendRequest(HttpMethod.GET, "https://mediacru.sh/api/" + hash);

                if (!string.IsNullOrEmpty(response))
                {
                    MediaCrushBlob blob = JsonConvert.DeserializeObject <MediaCrushBlob>(response);

                    return(new UploadResult(response)
                    {
                        URL = blob.DirectURL,
                        DeletionURL = blob.DeletionURL
                    });
                }
            }
            catch
            {
            }

            return(null);
        }
Exemple #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);
        }