Esempio n. 1
0
        private UploadResult HandleDuplicate(HttpWebResponse httpResponse)
        {
            JToken response;

            using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                response = JToken.Parse(streamReader.ReadToEnd());
            }
            string      hash = response["hash"].Value <string>();
            ImgrushBlob blob = response[hash].ToObject <ImgrushBlob>();

            return(new UploadResult
            {
                URL = DirectLink ? blob.DirectURL : blob.URL,
                DeletionURL = blob.DeletionURL
            });
        }
Esempio n. 2
0
        private UploadResult CheckExists(string hash)
        {
            try
            {
                string response = SendRequest(HttpMethod.GET, "https://imgrush.com/api/" + hash);

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

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

            return(null);
        }
Esempio n. 3
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://imgrush.com/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://imgrush.com/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":
                    ImgrushBlob blob = jsonResponse[hash].ToObject <ImgrushBlob>();
                    result.URL         = DirectLink ? blob.DirectURL : blob.URL;
                    result.DeletionURL = blob.DeletionURL;
                    return(result);

                case "unrecognized":
                    // Note: Imgrush 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);
        }