private async void OnChooseFile()
        {
            var file = Dialogs.SelectImageFile();

            if (string.IsNullOrEmpty(file))
            {
                return;
            }

            var attachment = new AttachmentViewModel(this)
            {
                FilePath = file
            };

            Attachments.Add(attachment); // updates UI

            var response = await Model.PostImage <Response>(attachment, true);

            if (response.content == null)
            {
                StatusBarManager.StatusLabel.Text = "Failed to upload. Please try again.";
                Attachments.Remove(attachment);
                return;
            }

            StatusBarManager.StatusLabel.Text = "Successfully uploaded: " + response.content.name;
            attachment.HtmlLink           = "\n![image](" + response.content.html_url + "?raw=true)\n";
            attachment.UploadImageContent = response.content;

            Feedback += attachment.HtmlLink;
        }
        private async void OnWindowKeyDown(KeyEventArgs args)
        {
            // (Konrad) Only handle CTRL + V
            if (args.Key != Key.V || Keyboard.Modifiers != ModifierKeys.Control)
            {
                return;
            }
            if (!Clipboard.ContainsImage())
            {
                return;
            }

            var image = Clipboard.GetImage();

            if (image == null)
            {
                return;
            }

            var tempFile = Path.Combine(Path.GetTempPath(), DateTime.Now.ToString("yyyyMMddTHHmmss") + "_ClipboardFile.jpg");

            using (var fileStream = new FileStream(tempFile, FileMode.Create))
            {
                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(image));
                encoder.Save(fileStream);
            }

            if (!File.Exists(tempFile))
            {
                return;
            }

            var attachment = new AttachmentViewModel(this)
            {
                FilePath = tempFile
            };

            Attachments.Add(attachment); // updates UI

            var response = await Model.PostImage <Response>(attachment, false);

            if (response.content == null)
            {
                StatusBarManager.StatusLabel.Text = "Failed to upload. Please try again.";
                Attachments.Remove(attachment);
                return;
            }

            StatusBarManager.StatusLabel.Text = "Successfully uploaded: " + response.content.name;
            attachment.HtmlLink           = "\n![image](" + response.content.html_url + "?raw=true)\n";
            attachment.UploadImageContent = response.content;

            Feedback += attachment.HtmlLink;
        }
        public async void DeleteAttachment(AttachmentViewModel vm)
        {
            Attachments.Remove(vm);
            var currentState = string.Copy(Feedback);

            Feedback = Feedback.Replace(vm.HtmlLink.Replace(Environment.NewLine, ""), "");

            var response = await Model.RemoveImage <Response>(vm);

            if (response.commit == null)
            {
                Attachments.Add(vm);
                Feedback = currentState;
                StatusBarManager.StatusLabel.Text = "Failed to remove image. Please try again.";
                return;
            }

            StatusBarManager.StatusLabel.Text = "Successfully removed: " + vm.UploadImageContent.name;
        }
Esempio n. 4
0
        /// <summary>
        /// Async call to GitHub that removes an image.
        /// </summary>
        /// <typeparam name="T">Type of response object.</typeparam>
        /// <param name="att">Attachment object to be removed.</param>
        /// <returns>Response object.</returns>
        public async Task <T> RemoveImage <T>(AttachmentViewModel att) where T : new()
        {
            // (Konrad) Apparently it's possible that new Windows updates change the standard
            // SSL protocol to SSL3. RestSharp uses whatever current one is while GitHub server
            // is not ready for it yet, so we have to use TLS1.2 explicitly.
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls12;

            var client  = new RestClient(baseUrl);
            var request = new RestRequest(Settings.FeedbackPath + "contents/" + att.UploadImageContent.path, Method.DELETE)
            {
                OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }
            };

            request.AddHeader("Content-type", "application/json");
            request.AddHeader("Authorization", "Token " + Settings.FeedbackToken);
            request.RequestFormat = DataFormat.Json;

            request.AddBody(new DeleteObject
            {
                path    = att.UploadImageContent.path,
                message = "removing an image",
                sha     = att.UploadImageContent.sha,
                branch  = "master"
            });

            var response = await client.ExecuteTaskAsync <T>(request);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                Log.AppendLog(LogMessageType.EXCEPTION, response.StatusDescription);
                return(new T());
            }

            return(response.Data);
        }
Esempio n. 5
0
        /// <summary>
        /// Async call to GitHub that adds an image.
        /// </summary>
        /// <typeparam name="T">Type of response object.</typeparam>
        /// <param name="att">Attachment object to be added.</param>
        /// <param name="createTemp"></param>
        /// <returns>Response object.</returns>
        public async Task <T> PostImage <T>(AttachmentViewModel att, bool createTemp) where T : new()
        {
            string tempFile;

            if (createTemp)
            {
                if (!File.Exists(att.FilePath))
                {
                    return(new T());
                }

                try
                {
                    tempFile = Path.Combine(Path.GetTempPath(), DateTime.Now.ToString("yyyyMMddTHHmmss") + "_" + Path.GetFileName(att.FilePath));
                    File.Copy(att.FilePath, tempFile);
                }
                catch (Exception e)
                {
                    Log.AppendLog(LogMessageType.EXCEPTION, e.Message);
                    return(new T());
                }
            }
            else
            {
                tempFile = att.FilePath;
            }

            var bytes = File.ReadAllBytes(tempFile);
            var body  = new UploadObject
            {
                path    = Path.Combine("images", Path.GetFileName(tempFile)),
                message = "uploading an image",
                content = Convert.ToBase64String(bytes),
                branch  = "master"
            };

            // (Konrad) Apparently it's possible that new Windows updates change the standard
            // SSL protocol to SSL3. RestSharp uses whatever current one is while GitHub server
            // is not ready for it yet, so we have to use TLS1.2 explicitly.
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls12;

            var client  = new RestClient(baseUrl);
            var request = new RestRequest(Settings.FeedbackPath + "contents/" + body.path, Method.PUT)
            {
                OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }
            };

            request.AddHeader("Content-type", "application/json");
            request.AddHeader("Authorization", "Token " + Settings.FeedbackToken);
            request.RequestFormat = DataFormat.Json;
            request.AddBody(body);

            try
            {
                File.Delete(tempFile);
            }
            catch (Exception e)
            {
                Log.AppendLog(LogMessageType.EXCEPTION, e.Message);
            }

            var response = await client.ExecuteTaskAsync <T>(request);

            if (response.StatusCode != HttpStatusCode.Created)
            {
                Log.AppendLog(LogMessageType.EXCEPTION, response.StatusDescription);
                return(new T());
            }

            return(response.Data);
        }