-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
88 lines (83 loc) · 2.23 KB
/
main.cpp
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
#include <algorithm>
#include <iostream>
#include "Imupager.h"
char* getCmdOption(char** begin, char** end, const std::string & option);
bool copyToClipboard(std::string str);
int main(int argc, char * argv[])
{
if (argc > 0)
{
char* path = getCmdOption(argv, argv + argc, "-f"); //getting the image path
char* url = getCmdOption(argv, argv + argc, "-u");
Imupager up;
up.initializeUpload();
//If user is sending a file
if (path)
{
HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
DWORD dwFileSize = GetFileSize(hFile, NULL);
void* pBuf = calloc(1, dwFileSize);
DWORD dwBytesRead = 0;
bool fResult = ReadFile(hFile, pBuf, dwFileSize, &dwBytesRead, NULL);
if (fResult)
{
std::cout << "File loaded. Size: " << dwFileSize << std::endl;
std::cout << "Sending POST" << std::endl;
up.uploadFile((void*)pBuf, dwFileSize);
}
else
std::cout << "Could not read the file: " << GetLastError() << std::endl;
}
//If user is sending a url
else if (url)
{
std::cout << "Url loaded" << std::endl;
std::cout << "Sending POST" << std::endl;
up.uploadUrl(url);
}
//Checking if everything was successful then copying the result url to clipboard
if (up.uploadSuccessful())
{
std::string url = up.getUrl();
bool cSuccess = copyToClipboard(url);
if (cSuccess)
{
std::cout << "URL copied to clippboard successfully." << std::endl;
}
else
std::cout << "Error while copying to clipboard." << std::endl;
}
else
std::cout << "Upload not succesfull." << std::endl;
}
long close = 5000;
std::cout << "Application will close in " << close << " miliseconds." << std::endl;
Sleep(close);
return 0;
}
char* getCmdOption(char** begin, char** end, const std::string & option)
{
char** itr = std::find(begin, end, option);
if (itr != end && ++itr != end)
{
return *itr;
}
return 0;
}
bool copyToClipboard(std::string str)
{
OpenClipboard(NULL);
EmptyClipboard();
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, str.size());
if (!hg)
{
CloseClipboard();
return FALSE;
}
memcpy_s(GlobalLock(hg), str.size(), str.c_str(), str.size());
GlobalUnlock(hg);
SetClipboardData(CF_TEXT, hg);
CloseClipboard();
GlobalFree(hg);
return TRUE;
}