forked from topharley/SCREENS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessor.cs
146 lines (122 loc) · 3.59 KB
/
Processor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
using Screens.Instruments;
using Screens.Uploaders;
namespace Screens
{
public class Processor
{
public static readonly string SCREENS_FAILED = "SCREENS_FAILED";
private Uploader _uploader;
public Processor()
{
_uploader = UploaderFactory.Create(Settings.Current.UploaderType);
Settings.Current.Apply();
}
public void Process(Bitmap bitmap)
{
SaveLastScreenshot(bitmap);
bitmap = EditScreenshot(bitmap);
if (Settings.Current.DrawWaterMark)
bitmap = (Bitmap)TextInstrument.DrawWaterMark(bitmap);
string url = UploadScreenshot(bitmap);
if (Settings.Current.ShortenUrl) url = ShortenUrl(url);
var success = url != null && url.StartsWith("http");
if (success)
{
SetClipboard(url);
OpenBrowser(url);
LogScreenshotUrl(url);
}
SetSuccessToEnvironment(success, url);
if (!success)
MessageBox.Show("Failed to upload, try again later.");
Clipper.Terminate();
}
private void SetSuccessToEnvironment(bool success, string url)
{
Console.Write(success ? url : SCREENS_FAILED);
}
private static void OpenBrowser(string url)
{
if (Settings.Current.OpenInBrowser)
System.Diagnostics.Process.Start(url);
}
private static void SetClipboard(string url)
{
if (Settings.Current.CopyInClipboard)
{
try
{
Clipboard.SetText(url);
}
catch
{
Nop();
}
}
}
private static void Nop()
{
}
private string ShortenUrl(string url)
{
try
{
return Shortener.Shorten(url);
}
catch
{
return url;
}
}
private static Bitmap EditScreenshot(Bitmap bitmap)
{
EditorForm editorForm = new EditorForm(bitmap);
if (editorForm.ShowDialog() != DialogResult.OK) Clipper.Terminate();
return editorForm.Bitmap;
}
private static void SaveLastScreenshot(Bitmap bitmap)
{
if (Settings.Current.SaveLastScreenshot && Settings.Current.SaveLastScreenshotFile != null)
{
try
{
File.WriteAllBytes(Settings.Current.SaveLastScreenshotFile, Clipper.GetPng(bitmap));
}
catch
{
Nop();
}
}
}
private static void LogScreenshotUrl(string url)
{
if (Settings.Current.SaveLog && Settings.Current.SaveLogFile != null)
{
try
{
File.AppendAllText(Settings.Current.SaveLogFile, DateTime.Now + ": " + url + Environment.NewLine);
}
catch
{
Nop();
}
}
}
private string UploadScreenshot(Bitmap bitmap)
{
try
{
return _uploader.Upload(bitmap);
}
catch
{
return null;
}
}
}
}