This repository has been archived by the owner on Oct 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program-cli.cs
executable file
·304 lines (263 loc) · 9.14 KB
/
Program-cli.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Windows.Forms;
namespace Poof
{
class Program_cli
{
//private static string homeDir = @"C:\Users\Brad\Paste\";
private static string commandPrefix = @"-";
public static Boolean debug = true;
private static PasteDB db;
[STAThread] // Single-threaded for use of the clipboard
static void Main(string[] args)
{
// On first startup, set the homeDirectory (default to @"%userprofile%\Poof\")
if (Properties.Settings.Default.homeDirectory == "")
{
Program_cli.debugMsg("No home directory found. First start?");
string homeDir = System.Environment.GetEnvironmentVariable("userprofile") + @"\Poof\";
CmdUtil.colorWriteLine("Poof wants to set your paste directory to \"" + homeDir + "\"" + Environment.NewLine + ". If this is okay, press enter. Else, enter a new directory.", ConsoleColor.Green);
string input = System.Console.ReadLine();
if (input.Length > 0)
{
homeDir = input;
}
setHomeDirectory(homeDir);
Properties.Settings.Default.Save();
}
// On debug, set up my test directory
useTestDirectory();
try
{
// Initialize the database
db = new PasteDB(Path.Combine(Properties.Settings.Default.homeDirectory, "poof.accdb"));
if (!db.Connect())
{
db.makeDB();
db.Connect();
}
if (args.Length == 0)
{
// No arguments given. User doesn't know what they're doing; Display help text
displayHelp();
}
else if (
args[0].Length >= (commandPrefix.Length + 1) &&
args[0].Substring(0, commandPrefix.Length).Equals(commandPrefix)
)
{
// First argument is a command. Perform the command using all the remaining arguments
performCommand(args);
}
else
{
// search the arguments as a list of tags for the best match
List<String> tags = new List<string>();
foreach (String arg in args)
{
tags.Add(arg);
}
PasteDBRow pasteResult = db.getTopPasteByTags(tags);
if (pasteResult != null)
{
displayPaste(pasteResult.uploadAddress);
}
else
{
// no results
CmdUtil.colorWriteLine("Alas, no results were found.", ConsoleColor.DarkRed);
}
}
db.Close();
}
catch(Exception ex)
{
Program_cli.errorMsg(ex.ToString());
CmdUtil.displayFatalError();
}
CmdUtil.colorWriteLine("\n«poof!»", ConsoleColor.DarkMagenta);
}
static public void displayHelp()
{
CmdUtil.colorWriteLine("Poof responds to the following commands:" + System.Environment.NewLine, ConsoleColor.Green);
CmdUtil.colorWriteLine("\t" + commandPrefix + "s\tForce a scan of the directory, uploading any new pictures " + System.Environment.NewLine + "\t\tthat aren't in the database", ConsoleColor.Green);
CmdUtil.colorWriteLine("\t" + commandPrefix + "t\tTakes a filename followed by tags, and updates the image " + System.Environment.NewLine + "\t\twith those tags", ConsoleColor.Green);
CmdUtil.colorWriteLine("\t" + commandPrefix + "h\tSets the home directory (Will force a rescan)", ConsoleColor.Green);
CmdUtil.colorWriteLine("\t" + commandPrefix + "l\tlists the database's contents", ConsoleColor.Green);
}
static public void performCommand(string[] commands)
{
if (commands[0].Equals(commandPrefix + "s"))
{
// Scan library
Program_cli.debugMsg("Scan library selected.");
scanLibrary();
}
else if (commands[0].Equals(commandPrefix + "t"))
{
// Tag a picture
Program_cli.debugMsg("Tag a picture selected.");
// First argument following the tag flag is the filename
string filename = commands[1];
// Remaining arguments following the tag flag are the tags to append
// Copy the tags from an array to a List
string[] newTagsArray = new string[commands.Length - 2];
Array.Copy(commands, 2, newTagsArray, 0, commands.Length - 2);
List<String> newTags = new List<String>(newTagsArray);
if (db.addTagsToPictureByLocation(filename, newTags))
{
Program_cli.debugMsg("Tagged " + filename + " with " + newTags[0] + "..."); //STUB
}
else
{
Program_cli.errorMsg("Error tagging picture"); //STUB
}
}
else if (commands[0].Equals(commandPrefix + "h"))
{
// Set home directory
Program_cli.debugMsg("Set home directory selected.");
if (commands.Length == 1)
{
CmdUtil.colorWriteLine("Your current home directory is " + Properties.Settings.Default.homeDirectory, ConsoleColor.Green);
}
else
{
if (setHomeDirectory(commands[1]))
{
CmdUtil.colorWriteLine("Poof successfully changed the home directory to \"" + commands[1] + "\".", ConsoleColor.Green);
}
else
{
CmdUtil.colorWriteLine("Unfortunately, Poof can't set the home directory to a location that doesn't exist.", ConsoleColor.Green);
}
}
}
else if (commands[0].Equals(commandPrefix + "l"))
{
// Output the database
Program_cli.debugMsg("output database selected.");
outputDatabase(); //STUB
}
else
{
Program_cli.errorMsg("Poof doesn't understand that command. The list of supported commands are:");
displayHelp();
}
}
static private Boolean scanLibrary()
{
string[] fileTypes = {"*.jpg", "*.jpeg", "*.gif", "*.png"};
List<string> fileList = new List<string>();
Boolean success = true;
// Populate fileList with a list of all picture files in DefaultDir
// Useful for getting a total count of the files that are being scanned
foreach(String fileType in fileTypes)
{
String[] filenames = Directory.GetFiles(Properties.Settings.Default.homeDirectory, fileType);
foreach(String filename in filenames)
{
fileList.Add(filename);
}
}
// Proceed through the file list, adding any pictures that aren't already in the database
for (int count = 0; count < fileList.Count; count++)
{
// If file doesn't exist in database, attempt to upload the picture and store it in the database
//Console.WriteLine(db.getPasteByFilename(fileList[count]).TagsAsString);
if(db.getPasteByFilename(fileList[count]) == null)
{
try
{
CmdUtil.colorWriteLine("Image " + (count + 1) + "/" + fileList.Count + " : Uploading to Imgur...");
ImageUploadAPI imgur = new ImgurUpload();
String uploadAddress = imgur.UploadPicture(fileList[count]);
if (uploadAddress != null || uploadAddress != "")
{
PasteDBRow newRow = new PasteDBRow(0, fileList[count], imgur.UploadPicture(fileList[count]), "");
db.addRow(newRow);
}
else
{
throw new Exception("An error occured trying to upload the file.");
}
}
catch (Exception ex)
{
errorMsg(ex.ToString());
CmdUtil.colorWriteLine("Error uploading image " + (count+1) + " to Imgur", ConsoleColor.Red);
success = false;
}
}
else
{
CmdUtil.colorWriteLine("Image " + (count + 1) + "/" + fileList.Count + " : Already in database!");
}
}
return success;
}
static private Boolean setHomeDirectory(String newHomeDir)
{
if (!Directory.Exists(newHomeDir))
{
return false;
}
else
{
Properties.Settings.Default.homeDirectory = newHomeDir;
Properties.Settings.Default.Save();
return true;
}
}
static private Boolean outputDatabase() //TODO
{
//TODO: Output database in human-readable format
List<PasteDBRow> results = db.returnAll();
foreach (PasteDBRow row in results)
{
string filename = Path.GetFileName(row.location);
//Console.WriteLine(filename + "\t" + "\t" + row.uploadAddress);
//CmdUtil.colorWriteLine(filename + "\t" + "\t" + row.uploadAddress, ConsoleColor.Green);
CmdUtil.colorWrite(String.Format("{0,-30}", filename), ConsoleColor.White);
CmdUtil.colorWriteLine(String.Format("{0,30}", row.uploadAddress), ConsoleColor.Gray);
if(row.tags.Count > 0)
CmdUtil.colorWrite("\t" + String.Join(", ", row.tags), ConsoleColor.Gray);
System.Console.WriteLine(System.Environment.NewLine);
}
return true;
}
static private void displayPaste(string address)
{
CmdUtil.colorWrite("The address ");
CmdUtil.colorWrite(address, ConsoleColor.Black, ConsoleColor.Yellow);
CmdUtil.colorWrite(" was written to the clipboard.\n");
Clipboard.SetText(address);
}
static public void debugMsg(string message)
{
#if DEBUG
CmdUtil.displayDebug(message);
#endif
}
static public void errorMsg(string message)
{
#if DEBUG
CmdUtil.displayError(message);
#else
CmdUtil.displayError();
#endif
}
static public void useTestDirectory()
{
#if DEBUG
CmdUtil.displayDebug("Resetting the default db location to " + System.Environment.GetEnvironmentVariable("userprofile") + @"\Poof\");
setHomeDirectory(System.Environment.GetEnvironmentVariable("userprofile") + @"\Poof\");
#endif
}
}
}