summaryrefslogtreecommitdiff
path: root/plugins/Dropbox/src/dropbox_transfers.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/Dropbox/src/dropbox_transfers.cpp')
-rw-r--r--plugins/Dropbox/src/dropbox_transfers.cpp152
1 files changed, 59 insertions, 93 deletions
diff --git a/plugins/Dropbox/src/dropbox_transfers.cpp b/plugins/Dropbox/src/dropbox_transfers.cpp
index d46b0871ed..f97f6d2f01 100644
--- a/plugins/Dropbox/src/dropbox_transfers.cpp
+++ b/plugins/Dropbox/src/dropbox_transfers.cpp
@@ -1,6 +1,6 @@
#include "stdafx.h"
-void CDropbox::UploadFile(const char *path, const char *data, size_t size)
+char* CDropbox::UploadFile(const char *data, size_t size, char *path)
{
ptrA token(db_get_sa(NULL, MODULE, "TokenSecret"));
ptrA encodedPath(mir_utf8encode(path));
@@ -8,6 +8,14 @@ void CDropbox::UploadFile(const char *path, const char *data, size_t size)
NLHR_PTR response(request.Send(hNetlibConnection));
HandleJsonResponseError(response);
+
+ JSONNode root = JSONNode::parse(response->pData);
+ if (!root.empty())
+ {
+ JSONNode node = root.at("path_lower");
+ mir_strcpy(path, node.as_string().c_str());
+ }
+ return path;
}
void CDropbox::StartUploadSession(const char *data, size_t size, char *sessionId)
@@ -26,28 +34,30 @@ void CDropbox::StartUploadSession(const char *data, size_t size, char *sessionId
mir_strcpy(sessionId, node.as_string().c_str());
}
-void CDropbox::AppendToUploadSession(const char *data, size_t size, const char *sessionId, size_t &offset)
+void CDropbox::AppendToUploadSession(const char *data, size_t size, const char *sessionId, size_t offset)
{
ptrA token(db_get_sa(NULL, MODULE, "TokenSecret"));
AppendToUploadSessionRequest request(token, sessionId, offset, data, size);
NLHR_PTR response(request.Send(hNetlibConnection));
HandleJsonResponseError(response);
-
- JSONNode root = JSONNode::parse(response->pData);
- if (root.empty())
- return;
-
- offset = root.at("offset").as_int();
}
-void CDropbox::FinishUploadSession(const char *data, size_t size, const char *sessionId, size_t offset, const char *path)
+char* CDropbox::FinishUploadSession(const char *data, size_t size, const char *sessionId, size_t offset, char *path)
{
ptrA token(db_get_sa(NULL, MODULE, "TokenSecret"));
FinishUploadSessionRequest request(token, sessionId, offset, path, data, size);
NLHR_PTR response(request.Send(hNetlibConnection));
HandleJsonResponseError(response);
+
+ JSONNode root = JSONNode::parse(response->pData);
+ if (!root.empty())
+ {
+ JSONNode node = root.at("path_lower");
+ mir_strcpy(path, node.as_string().c_str());
+ }
+ return path;
}
void CDropbox::CreateFolder(const char *path)
@@ -85,15 +95,14 @@ UINT CDropbox::SendFilesAsync(void *owner, void *arg)
CDropbox *instance = (CDropbox*)owner;
FileTransferParam *ftp = (FileTransferParam*)arg;
- ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_INITIALISING, ftp->hProcess, 0);
-
try {
if (ftp->ptszFolders) {
for (int i = 0; ftp->ptszFolders[i]; i++) {
if (ftp->isTerminated)
throw DropboxException("Transfer was terminated");
- CMStringA path = PreparePath(ftp->ptszFolders[i]);
+ char path[MAX_PATH];
+ PreparePath(ftp->ptszFolders[i], path);
instance->CreateFolder(path);
if (!strchr(path, '\\')) {
char url[MAX_PATH];
@@ -103,98 +112,58 @@ UINT CDropbox::SendFilesAsync(void *owner, void *arg)
}
}
- for (int i = 0; ftp->pfts.ptszFiles[i]; i++) {
- if (ftp->isTerminated)
- throw DropboxException("Transfer was terminated");
+ ftp->First();
+ do
+ {
+ const TCHAR *fileName = &ftp->GetCurrentFileName()[ftp->relativePathStart];
+ uint64_t fileSize = ftp->GetCurrentFileSize();
- FILE *hFile = _tfopen(ftp->pfts.ptszFiles[i], _T("rb"));
- if (hFile == NULL)
- throw DropboxException("Unable to open file");
+ int chunkSize = ftp->GetCurrentFileChunkSize();
+ char *data = (char*)mir_calloc(chunkSize);
+ size_t size = ftp->ReadCurrentFile(data, chunkSize);
- const TCHAR *fileName = NULL;
- if (!ftp->relativePathStart)
- fileName = _tcsrchr(ftp->pfts.ptszFiles[i], L'\\') + 1;
- else
- fileName = &ftp->pfts.ptszFiles[i][ftp->relativePathStart];
+ size_t offset = 0;
+ char sessionId[32];
+ instance->StartUploadSession(data, size, sessionId);
- _fseeki64(hFile, 0, SEEK_END);
- uint64_t fileSize = _ftelli64(hFile);
- rewind(hFile);
+ offset += size;
+ ftp->Progress(size);
- ftp->pfts.currentFileNumber = i;
- ftp->pfts.currentFileSize = fileSize;
- ftp->pfts.currentFileProgress = 0;
- ftp->pfts.tszCurrentFile = _tcsrchr(ftp->pfts.ptszFiles[i], '\\') + 1;
+ for (size_t chunk = 0, size = 0; chunk < (fileSize / chunkSize) - 1; chunk++, size = 0)
+ {
+ ftp->CheckCurrentFile();
- size_t offset = 0;
- char sessionId[32];
- int chunkSize = DROPBOX_FILE_CHUNK_SIZE / 4;
- if (fileSize < 1024 * 1024)
- chunkSize = DROPBOX_FILE_CHUNK_SIZE / 20;
- else if (fileSize > 20 * 1024 * 1024)
- chunkSize = DROPBOX_FILE_CHUNK_SIZE;
-
- char *data = (char*)mir_alloc(chunkSize);
- while (!feof(hFile) && fileSize != offset) {
- try {
- if (ferror(hFile))
- throw DropboxException("Error while file sending");
-
- if (ftp->isTerminated)
- throw DropboxException("Transfer was terminated");
-
- size_t size = fread(data, sizeof(char), chunkSize, hFile);
-
- if (offset == 0) {
- instance->StartUploadSession(data, size, sessionId);
- offset += size;
- }
- else
- instance->AppendToUploadSession(data, size, sessionId, offset);
-
- ftp->pfts.currentFileProgress += size;
- ftp->pfts.totalProgress += size;
- }
- catch (DropboxException&) {
- mir_free(data);
- fclose(hFile);
- throw;
- }
+ size = ftp->ReadCurrentFile(data, chunkSize);
+ instance->AppendToUploadSession(data, size, sessionId, offset);
- ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_DATA, ftp->hProcess, (LPARAM)&ftp->pfts);
+ offset += size;
+ ftp->Progress(size);
}
- mir_free(data);
- fclose(hFile);
- if (ftp->pfts.currentFileProgress < ftp->pfts.currentFileSize)
- throw DropboxException("Transfer was terminated");
+ if (offset < fileSize)
+ size = ftp->ReadCurrentFile(data, fileSize - offset);
- CMStringA path = PreparePath(fileName);
- instance->FinishUploadSession(0, 0, sessionId, offset, path);
+ char path[MAX_PATH];
+ PreparePath(fileName, path);
+ instance->FinishUploadSession(data, size, sessionId, offset, path);
+
+ ftp->Progress(size);
if (!_tcschr(fileName, L'\\')) {
char url[MAX_PATH];
instance->CreateDownloadUrl(path, url);
ftp->AddUrl(url);
}
-
- ftp->pfts.currentFileProgress = ftp->pfts.currentFileSize;
-
- if (i < ftp->pfts.totalFiles - 1)
- ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ftp->hProcess, 0);
- }
+ } while (ftp->Next());
}
catch (DropboxException &ex) {
Netlib_Logf(instance->hNetlibConnection, "%s: %s", MODULE, ex.what());
- ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ftp->hProcess, 0);
+ ftp->SetStatus(ACKRESULT_FAILED);
return ACKRESULT_FAILED;
}
- ProtoBroadcastAck(MODULE, ftp->pfts.hContact, ACKTYPE_FILE, ACKRESULT_SUCCESS, ftp->hProcess, 0);
-
- mir_forkthread(&CDropbox::RequestAccountInfo, instance);
-
- return 0;
+ ftp->SetStatus(ACKRESULT_SUCCESS);
+ return ACKRESULT_SUCCESS;
}
UINT CDropbox::SendFilesAndReportAsync(void *owner, void *arg)
@@ -203,17 +172,14 @@ UINT CDropbox::SendFilesAndReportAsync(void *owner, void *arg)
FileTransferParam *ftp = (FileTransferParam*)arg;
int res = SendFilesAsync(owner, arg);
- if (res) {
- instance->transfers.remove(ftp);
- delete ftp;
- return res;
- }
-
- CMStringA urls;
- for (int i = 0; i < ftp->urlList.getCount(); i++)
- urls.AppendFormat("%s\r\n", ftp->urlList[i]);
+ if (res == ACKRESULT_SUCCESS)
+ {
+ CMStringA urls;
+ for (int i = 0; i < ftp->urlList.getCount(); i++)
+ urls.AppendFormat("%s\r\n", ftp->urlList[i]);
- instance->Report(ftp->hContact, urls.GetBuffer());
+ instance->Report(ftp->hContact, urls.GetBuffer());
+ }
instance->transfers.remove(ftp);
delete ftp;