Exemplo n.º 1
0
        private void buttonRemoveFromImgur_Click(object sender, EventArgs e)
        {
            int  count            = listBoxHistory.SelectedItems.Count;
            bool isMultipleImages = count > 1;
            int  currentCount     = 0;

            listBoxHistory.BeginUpdate();
            List <HistoryItem> selectedItems = new List <HistoryItem>(listBoxHistory.SelectedItems.Cast <HistoryItem>());

            foreach (HistoryItem item in selectedItems)
            {
                ++currentCount;

                if (item == null)
                {
                    return;
                }

                string balloon_image_counter_text = (isMultipleImages ? (currentCount.ToString() + "/" + count.ToString()) : string.Empty);
                string balloon_text = "Attempting to remove " + (item.album ? "album" : "image") + " " + balloon_image_counter_text + " from Imgur...";
                ShowBalloonTip(2000, "Hold on...", balloon_text, ToolTipIcon.None);
                if (item.album ? ImgurAPI.DeleteAlbum(item.deletehash, item.anonymous) : ImgurAPI.DeleteImage(item.deletehash, item.anonymous))
                {
                    ShowBalloonTip(2000, "Success!", "Removed " + (item.album ? "album" : "image") + " " + balloon_image_counter_text + " from Imgur and history", ToolTipIcon.None);
                    History.RemoveHistoryItem(item);
                }
                else
                {
                    ShowBalloonTip(2000, "Failed", "Failed to remove " + (item.album ? "album" : "image") + " " + balloon_image_counter_text + " from Imgur", ToolTipIcon.Error);
                }
            }
            listBoxHistory.EndUpdate();
        }
Exemplo n.º 2
0
        private void UploadAlbum(bool _Anonymous, string[] _Paths, string _AlbumTitle)
        {
            ShowBalloonTip(2000, "Hold on...", "Attempting to upload album to Imgur (this may take a while)...", ToolTipIcon.None);

            // Create the album
            APIResponses.AlbumResponse album_response = ImgurAPI.CreateAlbum(_AlbumTitle, _Anonymous);
            if (!album_response.Success)
            {
                ShowBalloonTip(2000, "Failed", "Could not create album (" + album_response.Status + "): " + album_response.ResponseData.Error.Message, ToolTipIcon.None, true);
                Statistics.GatherAndSend();
                return;
            }

            // If we did manage to create the album we can now try to add images to it
            int   succeeded_count = 0;
            int   failed_count    = 0;
            int   image_idx       = 0;
            Image album_thumbnail = null;

            foreach (string path in _Paths)
            {
                // Instead of loading every file into memory, peek only their header to check if they are valid images
                try
                {
                    Image img = Image.FromStream(new MemoryStream(File.ReadAllBytes(path)));

                    string title       = string.Empty;
                    string description = string.Empty;

                    FormattingHelper.FormattingContext format_context = new FormattingHelper.FormattingContext();
                    format_context.FilePath   = path;
                    format_context.AlbumIndex = ++image_idx;

                    APIResponses.ImageResponse resp = ImgurAPI.UploadImage(img, GetTitleString(format_context), GetDescriptionString(format_context), _Anonymous, ref album_response);

                    // If we haven't selected any thumbnail yet, or if the currently uploaded image has been turned into the cover image of the album, turn it into a thumbnail for the history view.
                    if (album_thumbnail == null || resp.ResponseData.Id == album_response.ResponseData.Cover)
                    {
                        album_thumbnail = GenerateThumbnailImage(img, pictureBoxHistoryThumb.Width, pictureBoxHistoryThumb.Height);
                    }

                    if (resp.Success)
                    {
                        succeeded_count++;
                    }
                    else
                    {
                        failed_count++;
                    }
                }
                catch (ArgumentException)
                {
                    ShowBalloonTip(2000, "Failed", "File (" + path + ") is not a valid image file.", ToolTipIcon.Error, true);
                }
                catch (FileNotFoundException)
                {
                    ShowBalloonTip(2000, "Failed", "Could not find image file on disk (" + path + "):", ToolTipIcon.Error, true);
                }
                catch (IOException)
                {
                    ShowBalloonTip(2000, "Failed", "Image is in use by another program (" + path + "):", ToolTipIcon.Error, true);
                }
            }

            // If no images managed to upload to the album we should delete it again
            if (failed_count == _Paths.Count())
            {
                ShowBalloonTip(2000, "Album upload cancelled", "All images failed to upload! Check the log for more info.", ToolTipIcon.Error, true);

                // Delete the album because we couldn't upload anything
                ImgurAPI.DeleteAlbum(album_response.ResponseData.DeleteHash, _Anonymous);

                Statistics.GatherAndSend();
                return;
            }

            // Did we succeed in uploading the album?
            if (album_response.Success)
            {
                // Clipboard calls can only be made on an STA thread, threading model is MTA when invoked from context menu
                if (System.Threading.Thread.CurrentThread.GetApartmentState() != System.Threading.ApartmentState.STA)
                {
                    this.Invoke(new Action(() =>
                                           Clipboard.SetText(Properties.Settings.Default.copyHttpsLinks
                            ? album_response.ResponseData.Link.Replace("http://", "https://")
                            : album_response.ResponseData.Link)));
                }
                else
                {
                    Clipboard.SetText(Properties.Settings.Default.copyHttpsLinks
                        ? album_response.ResponseData.Link.Replace("http://", "https://")
                        : album_response.ResponseData.Link);
                }

                ShowBalloonTip(2000, "Success!", Properties.Settings.Default.copyLinks ? "Link copied to clipboard" : "Upload placed in history: " + album_response.ResponseData.Link, ToolTipIcon.None);

                HistoryItem item = new HistoryItem();
                item.Timestamp   = DateTime.Now;
                item.Id          = album_response.ResponseData.Id;
                item.Link        = album_response.ResponseData.Link;
                item.Deletehash  = album_response.ResponseData.DeleteHash;
                item.Title       = album_response.ResponseData.Title;
                item.Description = album_response.ResponseData.Description;
                item.Anonymous   = _Anonymous;
                item.Album       = true;
                item.Thumbnail   = album_thumbnail;
                Invoke(new Action(() => History.StoreHistoryItem(item)));
            }
            else
            {
                ShowBalloonTip(2000, "Failed", "Could not upload album (" + album_response.Status + "): " + album_response.ResponseData.Error.Message, ToolTipIcon.None, true);
            }

            Statistics.GatherAndSend();
        }