Beispiel #1
0
        private void Window_Drop(object sender, DragEventArgs e)
        {
            Task.Run(async() =>
            {
                if (e.Data.GetDataPresent(DataFormats.Html))
                {
                    string html = e.Data.GetData(DataFormats.Html) as string;

                    html = html.Replace('\n', ' ');
                    html = html.Replace('\r', ' ');
                    html = html.Replace("&", "&");

                    var matchImage = new Regex(@"<img (.*)\/?>");
                    var matchSrc   = new Regex("src=[\"']([\\w:\\/\\\\\\.\\d-=\\?\\&\\%;]*)\"");

                    var imageMatch = matchImage.Match(html);
                    var img        = imageMatch.Groups[1].Value;

                    var srcMatch = matchSrc.Match(img);
                    var src      = srcMatch.Groups[1].Value;

                    var matchFileName = new Regex(@"([\w\d]+\.(gif|png|jpg|jpeg))");
                    var fileNameMatch = matchFileName.Match(src);
                    var fileName      = fileNameMatch.Captures[0].Value;

                    var type = Path.GetExtension(fileName);
                    switch (type)
                    {
                    case ".png":
                        break;

                    case ".jpeg":
                        break;

                    case ".jpg":
                        break;

                    case ".gif":
                        break;

                    default:
                        return;
                    }


                    string tempFileName = fileName + '-' + Guid.NewGuid().ToString();
                    string tempFilePath = Path.Combine(AppSettings.Main.TempDir, tempFileName);

                    WebClient client = new WebClient();
                    await client.DownloadFileTaskAsync(src, tempFilePath);

                    string newFilePath = Path.GetFullPath(Path.Combine(AppSettings.Main.InputDir, fileName));
                    if (File.Exists(newFilePath))
                    {
                        File.Delete(newFilePath);
                    }

                    File.Move(tempFilePath, newFilePath);
                }
            });

            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                var files = e.Data.GetData(DataFormats.FileDrop) as string[];

                switch (ViewModel.Settings.FileDragMethod)
                {
                case AppSettings.ImportFileMethod.Copy:
                    Logger.Verbose("Copying files that were dragged onto AutoWaifu");
                    foreach (var file in files)
                    {
                        var outputPath = AppRelativePath.CreateInput(Path.GetFileName(file));
                        Logger.Verbose("Copying {@InputFile} to {@OutputFile}", file, outputPath);
                        FileSystemHelper.RecursiveCopy(file, outputPath);
                    }
                    break;

                case AppSettings.ImportFileMethod.Move:
                    Logger.Verbose("Moving files that were dragged onto AutoWaifu");
                    foreach (var file in files)
                    {
                        var outputPath = AppRelativePath.CreateInput(Path.GetFileName(file));
                        Logger.Verbose("Moving {@InputFile} to {@OutputFile}", file, outputPath);
                        FileSystemHelper.RecursiveMove(file, outputPath);
                    }
                    break;
                }
            }
        }