summaryrefslogtreecommitdiff
path: root/plugins/CloudFile/src/Services/dropbox_service.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/CloudFile/src/Services/dropbox_service.cpp')
-rw-r--r--plugins/CloudFile/src/Services/dropbox_service.cpp148
1 files changed, 65 insertions, 83 deletions
diff --git a/plugins/CloudFile/src/Services/dropbox_service.cpp b/plugins/CloudFile/src/Services/dropbox_service.cpp
index 528cb8e386..41b72b9de3 100644
--- a/plugins/CloudFile/src/Services/dropbox_service.cpp
+++ b/plugins/CloudFile/src/Services/dropbox_service.cpp
@@ -168,13 +168,17 @@ void CDropboxService::CreateFolder(const std::string &path)
DropboxAPI::CreateFolderRequest request(token, path.c_str());
NLHR_PTR response(request.Send(m_hConnection));
- HandleHttpError(response);
+ if (HTTP_CODE_SUCCESS(response->resultCode)) {
+ GetJsonResponse(response);
+ return;
+ }
- // forder exists on server
- if (response->resultCode == HTTP_CODE_FORBIDDEN)
+ // forder exists on server
+ if (response->resultCode == HTTP_CODE_CONFLICT) {
return;
+ }
- GetJsonResponse(response);
+ HttpResponseToError(response);
}
auto CDropboxService::CreateSharedLink(const std::string &path)
@@ -183,16 +187,14 @@ auto CDropboxService::CreateSharedLink(const std::string &path)
DropboxAPI::CreateSharedLinkRequest shareRequest(token, path.c_str());
NLHR_PTR response(shareRequest.Send(m_hConnection));
- if (response == nullptr)
- throw Exception(HttpStatusToError());
-
- if (!HTTP_CODE_SUCCESS(response->resultCode) &&
- response->resultCode != HTTP_CODE_CONFLICT) {
- if (response->dataLength)
- throw Exception(response->pData);
- throw Exception(HttpStatusToError(response->resultCode));
+ if (response && HTTP_CODE_SUCCESS(response->resultCode)) {
+ JSONNode root = GetJsonResponse(response);
+ return root["url"].as_string();
}
+ if (!response || response->resultCode != HTTP_CODE_CONFLICT)
+ HttpResponseToError(response);
+
JSONNode root = JSONNode::parse(response->pData);
if (root.isnull())
throw Exception(HttpStatusToError());
@@ -217,93 +219,73 @@ auto CDropboxService::CreateSharedLink(const std::string &path)
return link.as_string();
}
-UINT CDropboxService::Upload(FileTransferParam *ftp)
+void CDropboxService::Upload(FileTransferParam *ftp)
{
- if (!IsLoggedIn())
- Login();
-
- try {
- if (ftp->IsFolder()) {
- T2Utf folderName(ftp->GetFolderName());
-
- auto path = PreparePath(folderName);
- CreateFolder(path);
-
- auto link = CreateSharedLink(path);
- ftp->AddSharedLink(link.c_str());
- }
-
- ftp->FirstFile();
- do
- {
- T2Utf fileName(ftp->GetCurrentRelativeFilePath());
- uint64_t fileSize = ftp->GetCurrentFileSize();
-
- size_t chunkSize = ftp->GetCurrentFileChunkSize();
- mir_ptr<char>chunk((char*)mir_calloc(chunkSize));
-
- std::string path;
- auto serverFolder = ftp->GetServerFolder();
- if (serverFolder) {
- char serverPath[MAX_PATH] = { 0 };
- mir_snprintf(serverPath, "%s\\%s", T2Utf(serverFolder), fileName);
- path = PreparePath(serverPath);
- }
- else
- path = PreparePath(fileName);
+ std::string serverFolder = T2Utf(ftp->GetServerDirectory());
+ if (!serverFolder.empty()) {
+ auto path = PreparePath(serverFolder);
+ auto link = CreateSharedLink(path);
+ ftp->AddSharedLink(link.c_str());
+ }
- if (chunkSize == fileSize) {
- ftp->CheckCurrentFile();
- size_t size = ftp->ReadCurrentFile(chunk, chunkSize);
+ ftp->FirstFile();
+ do
+ {
+ std::string fileName = T2Utf(ftp->GetCurrentRelativeFilePath());
+ uint64_t fileSize = ftp->GetCurrentFileSize();
- path = UploadFile(chunk, size, path);
+ size_t chunkSize = ftp->GetCurrentFileChunkSize();
+ mir_ptr<char> chunk((char*)mir_calloc(chunkSize));
- ftp->Progress(size);
- }
- else {
- ftp->CheckCurrentFile();
- size_t size = ftp->ReadCurrentFile(chunk, chunkSize);
+ std::string path;
+ if (!serverFolder.empty())
+ path = "/" + serverFolder + "/" + fileName;
+ else
+ path = PreparePath(fileName);
- auto sessionId = CreateUploadSession(chunk, size);
+ if (chunkSize == fileSize) {
+ ftp->CheckCurrentFile();
+ size_t size = ftp->ReadCurrentFile(chunk, chunkSize);
- ftp->Progress(size);
+ path = UploadFile(chunk, size, path);
- size_t offset = size;
- double chunkCount = ceil(double(fileSize) / chunkSize) - 2;
- for (size_t i = 0; i < chunkCount; i++) {
- ftp->CheckCurrentFile();
+ ftp->Progress(size);
+ }
+ else {
+ ftp->CheckCurrentFile();
+ size_t size = ftp->ReadCurrentFile(chunk, chunkSize);
- size = ftp->ReadCurrentFile(chunk, chunkSize);
- UploadFileChunk(sessionId, chunk, size, offset);
+ auto sessionId = CreateUploadSession(chunk, size);
- offset += size;
- ftp->Progress(size);
- }
+ ftp->Progress(size);
+ size_t offset = size;
+ double chunkCount = ceil(double(fileSize) / chunkSize) - 2;
+ for (size_t i = 0; i < chunkCount; i++) {
ftp->CheckCurrentFile();
- size = offset < fileSize
- ? ftp->ReadCurrentFile(chunk, fileSize - offset)
- : 0;
- path = CommitUploadSession(sessionId, chunk, size, offset, path);
+ size = ftp->ReadCurrentFile(chunk, chunkSize);
+ UploadFileChunk(sessionId, chunk, size, offset);
+ offset += size;
ftp->Progress(size);
}
- if (!ftp->IsFolder()) {
- auto link = CreateSharedLink(path);
- ftp->AddSharedLink(link.c_str());
- }
- } while (ftp->NextFile());
- }
- catch (Exception &ex) {
- debugLogA("%s: %s", GetModuleName(), ex.what());
- ftp->SetStatus(ACKRESULT_FAILED);
- return ACKRESULT_FAILED;
- }
+ ftp->CheckCurrentFile();
+ size = offset < fileSize
+ ? ftp->ReadCurrentFile(chunk, fileSize - offset)
+ : 0;
+
+ path = CommitUploadSession(sessionId, chunk, size, offset, path);
+
+ ftp->Progress(size);
+ }
- ftp->SetStatus(ACKRESULT_SUCCESS);
- return ACKRESULT_SUCCESS;
+ if (!ftp->IsCurrentFileInSubDirectory()) {
+ auto link = CreateSharedLink(path);
+ ftp->AddSharedLink(link.c_str());
+ }
+ } while (ftp->NextFile());
}
/////////////////////////////////////////////////////////////////////////////////////////
@@ -318,4 +300,4 @@ struct CMPluginDropbox : public PLUGIN<CMPluginDropbox>
RegisterProtocol(PROTOTYPE_PROTOWITHACCS, (pfnInitProto)CDropboxService::Init, (pfnUninitProto)CDropboxService::UnInit);
}
}
- g_pluginDropbox;
+g_pluginDropbox;