/// <summary> /// Upload the capture to imgur /// </summary> /// <param name="captureDetails">ICaptureDetails</param> /// <param name="surfaceToUpload">ISurface</param> /// <param name="uploadUrl">out string for the url</param> /// <returns>true if the upload succeeded</returns> public bool Upload(ICaptureDetails captureDetails, ISurface surfaceToUpload, out string uploadUrl) { SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(_config.UploadFormat, _config.UploadJpegQuality, _config.UploadReduceColors); try { string filename = Path.GetFileName(FilenameHelper.GetFilenameFromPattern(_config.FilenamePattern, _config.UploadFormat, captureDetails)); ImgurInfo imgurInfo = null; // Run upload in the background new PleaseWaitForm().ShowAndWait("Imgur plug-in", Language.GetString("imgur", LangKey.communication_wait), delegate { imgurInfo = ImgurUtils.UploadToImgur(surfaceToUpload, outputSettings, captureDetails.Title, filename); if (imgurInfo != null && _config.AnonymousAccess) { Log.InfoFormat("Storing imgur upload for hash {0} and delete hash {1}", imgurInfo.Hash, imgurInfo.DeleteHash); _config.ImgurUploadHistory.Add(imgurInfo.Hash, imgurInfo.DeleteHash); _config.runtimeImgurHistory.Add(imgurInfo.Hash, imgurInfo); CheckHistory(); } } ); if (imgurInfo != null) { // TODO: Optimize a second call for export using (Image tmpImage = surfaceToUpload.GetImageForExport()) { imgurInfo.Image = ImageHelper.CreateThumbnail(tmpImage, 90, 90); } IniConfig.Save(); if (_config.UsePageLink) { uploadUrl = imgurInfo.Page; } else { uploadUrl = imgurInfo.Original; } if (!string.IsNullOrEmpty(uploadUrl) && _config.CopyLinkToClipboard) { try { ClipboardHelper.SetClipboardData(uploadUrl); } catch (Exception ex) { Log.Error("Can't write to clipboard: ", ex); uploadUrl = null; } } return(true); } } catch (Exception e) { Log.Error("Error uploading.", e); MessageBox.Show(Language.GetString("imgur", LangKey.upload_failure) + " " + e.Message); } uploadUrl = null; return(false); }
private string CreateNewFilename(ICaptureDetails captureDetails) { string fullPath = null; Log.Info().WriteLine("Creating new filename"); CoreConfiguration.ValidateAndCorrect(); var filename = FilenameHelper.GetFilenameFromPattern(CoreConfiguration.OutputFileFilenamePattern, CoreConfiguration.OutputFileFormat, captureDetails); var filepath = FilenameHelper.FillVariables(CoreConfiguration.OutputFilePath, false); try { fullPath = Path.Combine(filepath, filename); } catch (ArgumentException) { // configured filename or path not valid, show error message... Log.Info().WriteLine("Generated path or filename not valid: {0}, {1}", filepath, filename); MessageBox.Show(GreenshotLanguage.ErrorSaveInvalidChars, GreenshotLanguage.Error); // TODO: offer settings? // ... lets get the pattern fixed.... // fullPath = CreateNewFilename(captureDetails); // ... cancelled. // fullPath = null; } return(fullPath); }
/// <summary> /// Upload the capture to OneDrive /// </summary> /// <param name="captureDetails">ICaptureDetails</param> /// <param name="surfaceToUpload">ISurface</param> /// <returns>Uri</returns> private async Task <Uri> Upload(ICaptureDetails captureDetails, ISurface surfaceToUpload) { var outputSettings = new SurfaceOutputSettings(_config.UploadFormat, _config.UploadJpegQuality, _config.UploadReduceColors); try { string filename = Path.GetFileName(FilenameHelper.GetFilenameFromPattern(_config.FilenamePattern, _config.UploadFormat, captureDetails)); Uri response = null; var cancellationTokenSource = new CancellationTokenSource(); using (var pleaseWaitForm = new PleaseWaitForm("OneDrive plug-in", _oneDriveLanguage.CommunicationWait, cancellationTokenSource)) { pleaseWaitForm.Show(); try { var oneDriveResponse = await OneDriveUtils.UploadToOneDriveAsync(_oauth2Settings, surfaceToUpload, outputSettings, captureDetails.Title, filename, null, cancellationTokenSource.Token); response = new Uri(oneDriveResponse.WebUrl); } finally { pleaseWaitForm.Close(); } } if (response != null) { if (_config.AfterUploadLinkToClipBoard) { ClipboardHelper.SetClipboardData(response.ToString()); } return(response); } } catch (Exception e) { Log.Error().WriteLine(e, "Error uploading."); MessageBox.Show(_oneDriveLanguage.UploadFailure + " " + e.Message); } return(null); }
public override bool ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) { bool outputMade = false; string pattern = "${title}"; if (string.IsNullOrEmpty(pattern)) { pattern = "greenshot ${capturetime}"; } string filename = FilenameHelper.GetFilenameFromPattern(pattern, conf.OutputFileFormat, captureDetails); string filepath = FilenameHelper.FillVariables(conf.OutputFilePath, false); string fullPath = Path.Combine(filepath, filename); // Catching any exception to prevent that the user can't write in the directory. // This is done for e.g. bugs #2974608, #2963943, #2816163, #2795317, #2789218, #3004642 using (Image image = surface.GetImageForExport()) { try { // TODO: For now we overwrite, but this should be fixed some time ImageOutput.Save(image, fullPath, true); outputMade = true; } catch (Exception e) { LOG.Error("Error saving screenshot!", e); // Show the problem MessageBox.Show(Language.GetString(LangKey.error_save), Language.GetString(LangKey.error)); // when save failed we present a SaveWithDialog fullPath = ImageOutput.SaveWithDialog(image, captureDetails); outputMade = (fullPath != null); } } // Don't overwite filename if no output is made if (outputMade) { surface.LastSaveFullPath = fullPath; surface.Modified = false; captureDetails.Filename = fullPath; surface.SendMessageEvent(this, SurfaceMessageTyp.FileSaved, Language.GetFormattedString(LangKey.editor_imagesaved, surface.LastSaveFullPath)); } else { surface.SendMessageEvent(this, SurfaceMessageTyp.Info, ""); } return(outputMade); }
private bool CheckFilenamePattern() { var filename = FilenameHelper.GetFilenameFromPattern(textbox_screenshotname.Text, coreConfiguration.OutputFileFormat, null); // we allow dynamically created subfolders, need to check for them, too var pathParts = filename.Split(Path.DirectorySeparatorChar); var filenamePart = pathParts[pathParts.Length - 1]; var settingsOk = FilenameHelper.IsFilenameValid(filenamePart); for (var i = 0; settingsOk && i < pathParts.Length - 1; i++) { settingsOk = FilenameHelper.IsDirectoryNameValid(pathParts[i]); } DisplayTextBoxValidity(textbox_screenshotname, settingsOk); return(settingsOk); }
private string CreateNewFilename(ICaptureDetails captureDetails) { string fullPath; Log.Info().WriteLine("Creating new filename"); var pattern = _coreConfiguration.OutputFileFilenamePattern; if (string.IsNullOrEmpty(pattern)) { pattern = "greenshot ${capturetime}"; } var filename = FilenameHelper.GetFilenameFromPattern(pattern, _coreConfiguration.OutputFileFormat, captureDetails); _coreConfiguration.ValidateAndCorrectOutputFilePath(); var filepath = FilenameHelper.FillVariables(_coreConfiguration.OutputFilePath, false); try { fullPath = Path.Combine(filepath, filename); } catch (ArgumentException) { // configured filename or path not valid, show error message... Log.Info().WriteLine("Generated path or filename not valid: {0}, {1}", filepath, filename); MessageBox.Show(Language.GetString(LangKey.error_save_invalid_chars), Language.GetString(LangKey.error)); // ... lets get the pattern fixed.... var dialogResult = _settingsForm.ShowDialog(); if (dialogResult == DialogResult.OK) { // ... OK -> then try again: fullPath = CreateNewFilename(captureDetails); } else { // ... cancelled. fullPath = null; } } return(fullPath); }
void CaptureImage(Image img) { DoCaptureFeedback(); this.Hide(); string fullPath = null; ImageOutput.PrepareClipboardObject(); if ((conf.Output_Destinations & ScreenshotDestinations.FileDefault) == ScreenshotDestinations.FileDefault) { string filename = FilenameHelper.GetFilenameFromPattern(conf.Output_File_FilenamePattern, conf.Output_File_Format); fullPath = Path.Combine(conf.Output_File_Path, filename); ImageOutput.Save(img, fullPath); } if ((conf.Output_Destinations & ScreenshotDestinations.FileWithDialog) == ScreenshotDestinations.FileWithDialog) { fullPath = ImageOutput.SaveWithDialog(img); } if ((conf.Output_Destinations & ScreenshotDestinations.Clipboard) == ScreenshotDestinations.Clipboard) { ImageOutput.CopyToClipboard(img); } if ((conf.Output_Destinations & ScreenshotDestinations.Printer) == ScreenshotDestinations.Printer) { new PrintHelper(img).PrintWithDialog(); } if ((conf.Output_Destinations & ScreenshotDestinations.Editor) == ScreenshotDestinations.Editor) { ImageEditorForm editor = new ImageEditorForm(); editor.SetImage(img); if (fullPath != null) { editor.SetImagePath(fullPath); } editor.Show(); } }
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) { ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description); bool outputMade; bool overwrite; string fullPath; // Get output settings from the configuration SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(); if (captureDetails != null && captureDetails.Filename != null) { // As we save a pre-selected file, allow to overwrite. overwrite = true; LOG.InfoFormat("Using previous filename"); fullPath = captureDetails.Filename; outputSettings.Format = ImageOutput.FormatForFilename(fullPath); } else { LOG.InfoFormat("Creating new filename"); string pattern = conf.OutputFileFilenamePattern; if (string.IsNullOrEmpty(pattern)) { pattern = "greenshot ${capturetime}"; } string filename = FilenameHelper.GetFilenameFromPattern(pattern, conf.OutputFileFormat, captureDetails); string filepath = FilenameHelper.FillVariables(conf.OutputFilePath, false); fullPath = Path.Combine(filepath, filename); // As we generate a file, the configuration tells us if we allow to overwrite overwrite = conf.OutputFileAllowOverwrite; } if (conf.OutputFilePromptQuality) { QualityDialog qualityDialog = new QualityDialog(outputSettings); qualityDialog.ShowDialog(); } // Catching any exception to prevent that the user can't write in the directory. // This is done for e.g. bugs #2974608, #2963943, #2816163, #2795317, #2789218, #3004642 try { ImageOutput.Save(surface, fullPath, overwrite, outputSettings, conf.OutputFileCopyPathToClipboard); outputMade = true; } catch (ArgumentException ex1) { // Our generated filename exists, display 'save-as' LOG.InfoFormat("Not overwriting: {0}", ex1.Message); // when we don't allow to overwrite present a new SaveWithDialog fullPath = ImageOutput.SaveWithDialog(surface, captureDetails); outputMade = (fullPath != null); } catch (Exception ex2) { LOG.Error("Error saving screenshot!", ex2); // Show the problem MessageBox.Show(Language.GetString(LangKey.error_save), Language.GetString(LangKey.error)); // when save failed we present a SaveWithDialog fullPath = ImageOutput.SaveWithDialog(surface, captureDetails); outputMade = (fullPath != null); } // Don't overwite filename if no output is made if (outputMade) { exportInformation.ExportMade = outputMade; exportInformation.Filepath = fullPath; captureDetails.Filename = fullPath; conf.OutputFileAsFullpath = fullPath; } ProcessExport(exportInformation, surface); return(exportInformation); }
/// <summary> /// Generate a filename /// </summary> /// <param name="surface">ISurface</param> /// <param name="fileConfiguration">IFileConfiguration</param> /// <param name="destinationFileConfiguration">IDestinationFileConfiguration</param> /// <returns>Filename</returns> public static string GenerateFilename(this ISurface surface, IFileConfiguration fileConfiguration, IDestinationFileConfiguration destinationFileConfiguration = null) { var selectedFileConfiguration = fileConfiguration.Choose(destinationFileConfiguration); return(Path.GetFileName(FilenameHelper.GetFilenameFromPattern(selectedFileConfiguration.OutputFileFilenamePattern, selectedFileConfiguration.OutputFileFormat, surface.CaptureDetails))); }
/// <summary> /// Upload the capture to imgur /// </summary> /// <param name="captureDetails">ICaptureDetails</param> /// <param name="surfaceToUpload">ISurface</param> /// <returns>Uri</returns> private async Task <Uri> Upload(ICaptureDetails captureDetails, ISurface surfaceToUpload) { var outputSettings = new SurfaceOutputSettings(_imgurConfiguration.UploadFormat, _imgurConfiguration.UploadJpegQuality, _imgurConfiguration.UploadReduceColors); try { string filename = Path.GetFileName(FilenameHelper.GetFilenameFromPattern(_imgurConfiguration.FilenamePattern, _imgurConfiguration.UploadFormat, captureDetails)); ImgurImage imgurImage; var cancellationTokenSource = new CancellationTokenSource(); using (var pleaseWaitForm = new PleaseWaitForm("Imgur plug-in", _imgurLanguage.CommunicationWait, cancellationTokenSource)) { pleaseWaitForm.Show(); try { imgurImage = await ImgurUtils.UploadToImgurAsync(_oauth2Settings, surfaceToUpload, outputSettings, captureDetails.Title, filename, null, cancellationTokenSource.Token); if (imgurImage != null) { // Create thumbnail using (var tmpImage = surfaceToUpload.GetBitmapForExport()) using (var thumbnail = tmpImage.CreateThumbnail(90, 90)) { imgurImage.Image = thumbnail.ToBitmapSource(); } if (_imgurConfiguration.AnonymousAccess && _imgurConfiguration.TrackHistory) { Log.Info().WriteLine("Storing imgur upload for hash {0} and delete hash {1}", imgurImage.Data.Id, imgurImage.Data.Deletehash); _imgurConfiguration.ImgurUploadHistory.Add(imgurImage.Data.Id, imgurImage.Data.Deletehash); _imgurConfiguration.RuntimeImgurHistory.Add(imgurImage.Data.Id, imgurImage); // TODO: Update History - ViewModel!!! // UpdateHistoryMenuItem(); } } } finally { pleaseWaitForm.Close(); } } if (imgurImage != null) { var uploadUrl = _imgurConfiguration.UsePageLink ? imgurImage.Data.LinkPage: imgurImage.Data.Link; if (uploadUrl == null || !_imgurConfiguration.CopyLinkToClipboard) { return(uploadUrl); } try { ClipboardHelper.SetClipboardData(uploadUrl.AbsoluteUri); } catch (Exception ex) { Log.Error().WriteLine(ex, "Can't write to clipboard: "); uploadUrl = null; } return(uploadUrl); } } catch (Exception e) { Log.Error().WriteLine(e, "Error uploading."); MessageBox.Show(_imgurLanguage.UploadFailure + " " + e.Message); } return(null); }
/// <summary> /// Upload the capture to lutim /// </summary> /// <param name="captureDetails">ICaptureDetails</param> /// <param name="surfaceToUpload">ISurface</param> /// <param name="uploadUrl">out string for the url</param> /// <returns>true if the upload succeeded</returns> private async Task <string> Upload(ICaptureDetails captureDetails, ISurface surfaceToUpload) { string uploadUrl; SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(_lutimConfiguration.UploadFormat, _lutimConfiguration.UploadJpegQuality, _lutimConfiguration.UploadReduceColors); try { string filename = Path.GetFileName(FilenameHelper.GetFilenameFromPattern(_lutimConfiguration.FilenamePattern, _lutimConfiguration.UploadFormat, captureDetails)); LutimInfo lutimInfo; var cancellationTokenSource = new CancellationTokenSource(); using (var pleaseWaitForm = new PleaseWaitForm("Lutim", _lutimLanguage.CommunicationWait, cancellationTokenSource)) { pleaseWaitForm.Show(); try { lutimInfo = await LutimUtils.UploadToLutim(surfaceToUpload, outputSettings, filename); if (lutimInfo != null) { Log.Info().WriteLine("Storing lutim upload for hash {0} and delete hash {1}", lutimInfo.Short, lutimInfo.Token); // TODO: Write somewhere // _lutimConfiguration.LutimUploadHistory.Add(lutimInfo.Short, lutimInfo.ToIniString()); _lutimConfiguration.RuntimeLutimHistory.Add(lutimInfo.Short, lutimInfo); // TODO: Update // UpdateHistoryMenuItem(); } } finally { pleaseWaitForm.Close(); } } if (lutimInfo != null) { uploadUrl = lutimInfo.Short; if (string.IsNullOrEmpty(uploadUrl) || !_lutimConfiguration.CopyLinkToClipboard) { return(uploadUrl); } try { ClipboardHelper.SetClipboardData(uploadUrl); } catch (Exception ex) { Log.Error().WriteLine(ex, "Can't write to clipboard: "); return(null); } return(uploadUrl); } } catch (Exception e) { Log.Error().WriteLine(e, "Error uploading."); MessageBox.Show(_lutimLanguage.UploadFailure + " " + e.Message); } return(null); }