Example #1
0
        public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            progress.Status = "Taking a crop shot";

            CropShotWindow cropShotWindow = null;

            var thread = new Thread(() => {
                cropShotWindow = new CropShotWindow {
                    SourceImage = (Image) data.Data,
                    DataName = data.Name
                };

                cropShotWindow.ShowDialog();
            });

            // UI objects want to be in STA; make sure we're in STA
            thread.SetApartmentState(ApartmentState.STA);

            thread.Start();
            thread.Join();

            if (cropShotWindow.Data == null) {
                throw new CommandCanceledException(this);
            }

            progress.Progress = 1;

            return cropShotWindow.Data;
        }
Example #2
0
        public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            switch (data.DataType) {
                case TypedDataType.Stream:
                    progress.Status = "Saving to file";

                    string path = Path.Combine(RootPath ?? "", data.Name);

                    using (var file = File.Open(path, FileMode.Create, FileAccess.Write)) {
                        var inputStream = (Stream) data.Data;

                        CopyStream(inputStream, file, progress);

                        var uriBuilder = new UriBuilder {
                            Scheme = Uri.UriSchemeFile,
                            Path = path
                        };

                        return TypedData.FromUri(uriBuilder.Uri, "output file");
                    }

                default:
                    return null;
            }
        }
Example #3
0
        protected override MultipartData GetRequestData(TypedData data)
        {
            var builder = new MultipartDataBuilder();
            builder.KeyValuePair("longurl", data.Data.ToString());

            return builder.GetData();
        }
Example #4
0
        protected override MultipartData GetRequestData(TypedData data)
        {
            var builder = new MultipartDataBuilder();
            builder.KeyValuePair("key", Settings.Default.imgurApiKey);
            builder.KeyValuePair("name", data.Name);
            builder.File((Stream) data.Data, "image", data.Name);

            return builder.GetData();
        }
Example #5
0
        protected override TypedData GetResponseData(HttpWebResponse response, TypedData originalData)
        {
            var xmlDoc = HttpRequest.GetResponseXml(response);

            var nodes = xmlDoc.SelectNodes("/upload/links/original");

            if (nodes == null || nodes.Count == 0) {
                return null;
            }

            return TypedData.FromUri(nodes[0].InnerText, originalData.Name);
        }
Example #6
0
        public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            switch (SourceType) {
                case ScreenshotSourceType.EntireDesktop:
                    var image = ScreenCapturer.CaptureEntireDesktop();

                    progress.Progress = 1;  // Can't really track progress on this...

                    return TypedData.FromImage(image, "screenshot");

                default:
                    return null;
            }
        }
Example #7
0
        public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            Action operation;
            bool success = false;
            Exception exception = null;

            if (data == null) {
                operation = new Action(() => {
                    progress.Status = "Reading clipboard";

                    try {
                        data = GetClipboardData();
                        success = true;
                    } catch (Exception e) {
                        exception = e;
                    }
                });
            } else {
                operation = new Action(() => {
                    progress.Status = "Saving to clipboard";

                    try {
                        SetClipboardData(data);
                        success = true;
                    } catch (Exception e) {
                        exception = e;
                    }
                });
            }

            var thread = new Thread(new ThreadStart(operation));

            // Clipboard object uses COM; make sure we're in STA
            thread.SetApartmentState(ApartmentState.STA);

            thread.Start();
            thread.Join();

            if (!success) {
                if (exception == null) {
                    throw new CommandCanceledException(this);
                }

                throw exception;
            }

            progress.Progress = 1;

            return data;
        }
Example #8
0
        public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            progress.Status = "Uploading data";

            var galleryProgress = new MutableProgressTracker();
            var uploadProgress = new MutableProgressTracker();

            var aggregateProgress = new AggregateProgressTracker(new ProgressTrackerCollection {
                { galleryProgress, TimeEstimates.ShortOperation.ProgressWeight },
                { uploadProgress,  TimeEstimates.LongOperation.ProgressWeight },
            });

            aggregateProgress.BindTo(progress);

            var galleryAccessData = CreateGallery(galleryProgress, cancelToken);

            return UploadData(galleryAccessData, data, uploadProgress, cancelToken);
        }
Example #9
0
        protected override MultipartData GetRequestData(TypedData data)
        {
            var builder = new MultipartDataBuilder();
            builder.File((Stream) data.Data, "upload", data.Name);
            builder.KeyValuePairs(new Dictionary<string, string> {
                // FIXME Remove and adjust as required.  Just copy-pasted all
                // this from the main page.  (Don't shoot me.)
                { "mode", "local" },
                { "formurl", "http://www.postimage.org/" },
                { "tpl", "." },
                { "addform", "" },
                { "mforum", "" },
                { "um", "image" },
                { "adult", IsAdult ? "yes" : "no" },
                { "ui", "" },
                { "hash", "943" },
                { "optsize", "0" },
                { "submit", "Upload It!" },
            });

            return builder.GetData();
        }
Example #10
0
        private static TypedData UploadData(GalleryAccessData accessData, TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            var uri = new UriBuilder(string.Format(ApiUriFormat, "UploadItem")) {
                Query = HttpUtility.ToQueryString(new Dictionary<string, string> {
                    { "editor_id", accessData.EditorId },
                    { "key", accessData.Key },
                    { "filename", data.Name },
                }),
            }.Uri;

            var response = HttpRequest.Execute(uri, (Stream) data.Data, progress, cancelToken);
            var json = HttpRequest.GetResponseJson(response);

            string id = json.Value<string>("id");

            // min.us pulls this bullshit where everything after and including the last '.'
            // in the filename is appended to the URL.
            // =|
            string extension = Path.GetExtension(data.Name);

            if (!string.IsNullOrEmpty(extension)) {
                id += extension;
            }

            return TypedData.FromUri(string.Format(DownloadUriFormat, HttpUtility.UrlEncode(id)), data.Name);
        }
Example #11
0
        public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            string fileName = data.Name;
            var stream = (Stream) data.Data;

            UploadData(stream, fileName, progress, cancelToken);

            progress.Progress = 1;

            string result;
            bool success = TryGetResultString(fileName, out result);

            return TypedData.FromUri(new Uri(success && !string.IsNullOrWhiteSpace(result) ? result : data.Name, UriKind.Absolute), fileName);
        }
Example #12
0
 protected MultipartData GetRequestData(TypedData data)
 {
     return new MultipartDataBuilder()
         .File((Stream) data.Data, null, data.Name)
         .GetData();
 }
Example #13
0
        protected override MultipartData GetRequestData(TypedData data)
        {
            var builder = new MultipartDataBuilder();
            builder.KeyValuePairs(new Dictionary<string, string> {
                { "raw_paste", data.Data.ToString() },
                { "comment", "" },
                { "author", Author ?? "" },
                { "language", Language ?? "text" },
                { "permissions", IsPrivate ? "1" : "0" },
                { "desc", data.Name ?? "" },
                { "linenumbers", ShowLineNumbers ? "0" : "1" },
                { "expire", Expiration.TotalSeconds.ToString(NumberFormatInfo.InvariantInfo) },
                { "submit", "Submit Paste" },
                { "tabbing", "true" },
                { "tabtype", "real" },
            });

            return builder.GetData();
        }
Example #14
0
        protected override TypedData GetResponseData(HttpWebResponse response, TypedData originalData)
        {
            string html = HttpRequest.GetResponseText(response);

            // Sorry for using regexp to parse HTML, but
            // including an HTML parsing library as a
            // dependency is just too much

            var match = LinkInHtml.Match(html);

            if (!match.Success) {
                return null;
            }

            return TypedData.FromUri(match.Value, originalData.Name);
        }
Example #15
0
        public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            var newData = new TypedData(data.DataType, data.CloneData(), FormatName(data.Name));

            progress.Progress = 1;

            ++this.sequenceId;

            return newData;
        }
Example #16
0
        protected override MultipartData GetRequestData(TypedData data)
        {
            var builder = new MultipartDataBuilder();
            builder.File((Stream) data.Data, "f", data.Name);
            builder.KeyValuePairs(new Dictionary<string, string> {
                { "t", "file" },
                { "name", data.Name ?? "" },
                { "tags", Tags ?? "" },
                { "description", Description ?? "" },
                { "adult", IsPrivate ? "t" : "f" },
                { "sfile", "Upload" },
                { "url", "" },
            });

            return builder.GetData();
        }
Example #17
0
        public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            if (string.IsNullOrEmpty(UserName)) {
                throw new InvalidOperationException("User name must be set");
            }

            if (Password == null) {
                throw new InvalidOperationException("Password must be set");
            }

            var progressTrackerCollection = new ProgressTrackerCollection {
                { new MutableProgressTracker(), 1 },
                { new MutableProgressTracker(), 1 },
                { new MutableProgressTracker(), 2 },
                { new MutableProgressTracker(), 100 },
            };

            var progressTrackers = progressTrackerCollection.Select((item) => item.ProgressTracker).OfType<IMutableProgressTracker>().ToArray();

            var aggregateProgressTracker = new AggregateProgressTracker(progressTrackerCollection);
            aggregateProgressTracker.BindTo(progress);

            string token = FetchToken(progressTrackers[0], cancelToken);
            string sessionKey = FetchSessionKey(token, progressTrackers[1], cancelToken);

            string uploadId, uploadInfo;
            ulong maxFileSize;
            var uploadUri = FetchUploadUri(sessionKey, out uploadId, out uploadInfo, out maxFileSize, progressTrackers[2], cancelToken);

            var uri = UploadData(data, uploadUri, uploadId, uploadInfo, maxFileSize, progressTrackers[3], cancelToken);

            return TypedData.FromUri(uri, data.Name);
        }
Example #18
0
        private static Uri UploadData(TypedData data, Uri uri, string uploadId, string uploadInfo, ulong maxFileSize, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            var postData = new MultipartDataBuilder()
                .KeyValuePair("MAX_FILE_SIZE", maxFileSize.ToString())
                .KeyValuePair("UPLOAD_IDENTIFIER", uploadId)
                .KeyValuePair("extra_info", uploadInfo)
                .File((Stream) data.Data, "userfile", data.Name)
                .GetData();

            var response = HttpRequest.Execute(uri, postData, HttpRequestMethod.Post, progress, cancelToken);

            string text = HttpRequest.GetResponseText(response);
            var match = new Regex(@"\bfile_id=(?<FileId>\S+)\b").Match(text);

            return new Uri(string.Format(OutputFileUriFormat, match.Groups["FileId"].Value), UriKind.Absolute);
        }
Example #19
0
        public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            var parameters = new {
                FileName = data.Name,
            };

            var dataBuilder = new MultipartDataBuilder();

            foreach (var parameter in PostParameters) {
                if (parameter.Key == null) {
                    continue;
                }

                dataBuilder.KeyValuePair(parameter.Key, StringLib.HartFormatter.HartFormat(parameter.Value, parameters));
            }

            dataBuilder.File((Stream) data.Data, FileParameterName, data.Name);

            progress.Status = "Uploading data";

            var response = HttpRequest.Execute(Uri, dataBuilder.GetData(), HttpRequestMethod.Post, progress, cancelToken);

            return TypedData.FromUri(response.ResponseUri, data.Name);
        }
Example #20
0
 public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
 {
     return new CommandChain(ImageWriter, FileUploader).Process(data, progress, cancelToken);
 }
Example #21
0
        private static void SetClipboardData(TypedData data)
        {
            switch (data.DataType) {
                case TypedDataType.Text:
                case TypedDataType.Uri:
                    SetRawClipboardData(data.Data.ToString());

                    break;

                case TypedDataType.Image:
                    SetRawClipboardData(data.Data);

                    break;

                default:
                    // Do nothing

                    break;
            }
        }
Example #22
0
 public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
 {
     return InnerCommand.Process(data, progress, cancelToken);
 }