Beispiel #1
0
        private void HandleErrors(ImgurResponse response)
        {
            ImgurErrorData errorData = ((JObject)response.data).ToObject <ImgurErrorData>();

            if (errorData != null)
            {
                string errorMessage = string.Format("Status: {0}, Request: {1}, Error: {2}", response.status, errorData.request, errorData.error);
                Errors.Add(errorMessage);
            }
        }
Beispiel #2
0
        private UploadResult InternalUpload(Stream stream, string fileName, bool refreshTokenOnError)
        {
            Dictionary <string, string> args = new Dictionary <string, string>();
            NameValueCollection         headers;

            if (UploadMethod == AccountType.User)
            {
                if (!CheckAuthorization())
                {
                    return(null);
                }

                if (!string.IsNullOrEmpty(UploadAlbumID))
                {
                    args.Add("album", UploadAlbumID);
                }

                headers = GetAuthHeaders();
            }
            else
            {
                headers = new NameValueCollection();
                headers.Add("Authorization", "Client-ID " + AuthInfo.Client_ID);
            }

            WebExceptionReturnResponse = true;
            UploadResult result = UploadData(stream, "https://api.imgur.com/3/image", fileName, "image", args, headers);

            if (!string.IsNullOrEmpty(result.Response))
            {
                ImgurResponse imgurResponse = JsonConvert.DeserializeObject <ImgurResponse>(result.Response);

                if (imgurResponse != null)
                {
                    if (imgurResponse.success && imgurResponse.status == 200)
                    {
                        ImgurImageData imageData = ((JObject)imgurResponse.data).ToObject <ImgurImageData>();

                        if (imageData != null && !string.IsNullOrEmpty(imageData.link))
                        {
                            if (Config.DirectLink)
                            {
                                if (UseGIFV && !string.IsNullOrEmpty(imageData.gifv))
                                {
                                    result.URL = imageData.gifv;
                                }
                                else
                                {
                                    result.URL = imageData.link;
                                }
                            }
                            else
                            {
                                result.URL = "http://imgur.com/" + imageData.id;
                            }

                            string thumbnail = string.Empty;

                            switch (ThumbnailType)
                            {
                            case ImgurThumbnailType.Small_Square:
                                thumbnail = "s";
                                break;

                            case ImgurThumbnailType.Big_Square:
                                thumbnail = "b";
                                break;

                            case ImgurThumbnailType.Small_Thumbnail:
                                thumbnail = "t";
                                break;

                            case ImgurThumbnailType.Medium_Thumbnail:
                                thumbnail = "m";
                                break;

                            case ImgurThumbnailType.Large_Thumbnail:
                                thumbnail = "l";
                                break;

                            case ImgurThumbnailType.Huge_Thumbnail:
                                thumbnail = "h";
                                break;
                            }

                            result.ThumbnailURL = string.Format("http://i.imgur.com/{0}{1}.jpg", imageData.id, thumbnail); // Thumbnails always jpg
                            result.DeletionURL  = "http://imgur.com/delete/" + imageData.deletehash;
                        }
                    }
                    else
                    {
                        ImgurErrorData errorData = ((JObject)imgurResponse.data).ToObject <ImgurErrorData>();

                        if (errorData != null)
                        {
                            if (UploadMethod == AccountType.User && refreshTokenOnError &&
                                errorData.error.Equals("The access token provided is invalid.", StringComparison.InvariantCultureIgnoreCase) && RefreshAccessToken())
                            {
                                DebugHelper.WriteLine("Imgur access token refreshed, reuploading image.");

                                return(InternalUpload(stream, fileName, false));
                            }

                            string errorMessage = string.Format("Imgur upload failed: ({0}) {1}", imgurResponse.status, errorData.error);
                            Errors.Clear();
                            Errors.Add(errorMessage);
                        }
                    }
                }
            }

            return(result);
        }