From 6481053a2d97d73902b4ba86b7a06812cff48ae1 Mon Sep 17 00:00:00 2001 From: aunsane Date: Sun, 9 Apr 2017 17:38:37 +0300 Subject: Dropbox: temporary url option --- plugins/Dropbox/src/api/operations.h | 34 +++++++++++++++ plugins/Dropbox/src/dropbox.cpp | 17 ++++++-- plugins/Dropbox/src/dropbox.h | 3 +- plugins/Dropbox/src/dropbox_commands.cpp | 2 +- plugins/Dropbox/src/dropbox_options.cpp | 4 +- plugins/Dropbox/src/dropbox_options.h | 2 +- plugins/Dropbox/src/dropbox_transfers.cpp | 70 ++++++++++++++++++------------- plugins/Dropbox/src/dropbox_utils.cpp | 23 ++++++---- plugins/Dropbox/src/resource.h | 6 +-- plugins/Dropbox/src/version.h | 2 +- 10 files changed, 113 insertions(+), 50 deletions(-) (limited to 'plugins/Dropbox/src') diff --git a/plugins/Dropbox/src/api/operations.h b/plugins/Dropbox/src/api/operations.h index c451e4190c..d97dec731e 100644 --- a/plugins/Dropbox/src/api/operations.h +++ b/plugins/Dropbox/src/api/operations.h @@ -18,6 +18,40 @@ public: } }; +class CreateSharedLinkRequest : public HttpRequest +{ +public: + CreateSharedLinkRequest(const char *token, const char *path) : + HttpRequest(REQUEST_POST, DROPBOX_API_RPC "/sharing/create_shared_link_with_settings") + { + AddBearerAuthHeader(token); + AddHeader("Content-Type", "application/json"); + + JSONNode root(JSON_NODE); + root << JSONNode("path", path); + + json_string data = root.write(); + SetData(data.c_str(), data.length()); + } +}; + +class GetSharedLinkRequest : public HttpRequest +{ +public: + GetSharedLinkRequest(const char *token, const char *path) : + HttpRequest(REQUEST_POST, DROPBOX_API_RPC "/sharing/list_shared_links") + { + AddBearerAuthHeader(token); + AddHeader("Content-Type", "application/json"); + + JSONNode root(JSON_NODE); + root << JSONNode("path", path); + + json_string data = root.write(); + SetData(data.c_str(), data.length()); + } +}; + class SearchRequest : public HttpRequest { public: diff --git a/plugins/Dropbox/src/dropbox.cpp b/plugins/Dropbox/src/dropbox.cpp index 74dd9acee9..9bb8612e97 100644 --- a/plugins/Dropbox/src/dropbox.cpp +++ b/plugins/Dropbox/src/dropbox.cpp @@ -67,14 +67,23 @@ bool CDropbox::HasAccessToken() void CDropbox::RequestAccountInfo(void *p) { - CDropbox *self = (CDropbox*)p; + CDropbox *instance = (CDropbox*)p; - MCONTACT hContact = self->GetDefaultContact(); + MCONTACT hContact = instance->GetDefaultContact(); ptrA token(db_get_sa(NULL, MODULE, "TokenSecret")); GetCurrentAccountRequest request(token); - NLHR_PTR response(request.Send(self->hNetlibConnection)); - HandleJsonResponseError(response); + NLHR_PTR response(request.Send(instance->hNetlibConnection)); + + try + { + HandleHttpResponse(response); + } + catch (DropboxException &ex) + { + Netlib_Logf(instance->hNetlibConnection, "%s: %s", MODULE, ex.what()); + return; + } JSONNode root = JSONNode::parse(response->pData); if (root.empty()) diff --git a/plugins/Dropbox/src/dropbox.h b/plugins/Dropbox/src/dropbox.h index 32a58691ec..f9957cc5b3 100644 --- a/plugins/Dropbox/src/dropbox.h +++ b/plugins/Dropbox/src/dropbox.h @@ -114,7 +114,8 @@ private: static bool IsAccountIntercepted(const char *module); static char* HttpStatusToText(HTTP_STATUS status); - static void HandleJsonResponseError(NETLIBHTTPREQUEST *response); + static void HandleHttpResponse(NETLIBHTTPREQUEST *response); + static JSONNode HandleJsonResponse(NETLIBHTTPREQUEST *response); static MEVENT AddEventToDb(MCONTACT hContact, WORD type, DWORD flags, DWORD cbBlob, PBYTE pBlob); diff --git a/plugins/Dropbox/src/dropbox_commands.cpp b/plugins/Dropbox/src/dropbox_commands.cpp index c9eb2492ae..4388374458 100644 --- a/plugins/Dropbox/src/dropbox_commands.cpp +++ b/plugins/Dropbox/src/dropbox_commands.cpp @@ -160,7 +160,7 @@ void CDropbox::CommandDelete(void *arg) try { - HandleJsonResponseError(response); + HandleJsonResponse(response); CMStringA message(FORMAT, "%s %s", path, T2Utf(TranslateT("is deleted"))); ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0); diff --git a/plugins/Dropbox/src/dropbox_options.cpp b/plugins/Dropbox/src/dropbox_options.cpp index 1105d3e71a..a4fe771dad 100644 --- a/plugins/Dropbox/src/dropbox_options.cpp +++ b/plugins/Dropbox/src/dropbox_options.cpp @@ -5,10 +5,10 @@ CDropboxOptionsMain::CDropboxOptionsMain(CDropbox *instance) m_instance(instance), m_auth(this, IDC_GETAUTH, DROPBOX_WWW_URL "/oauth2/authorize?response_type=code&client_id=" DROPBOX_APP_KEY), m_requestCode(this, IDC_REQUEST_CODE), m_authorize(this, IDC_AUTHORIZE), m_authStatus(this, IDC_AUTH_STATUS), - m_useShortUrl(this, IDC_USE_SHORT_LINKS), m_urlAutoSend(this, IDC_URL_AUTOSEND), + m_urlIsTemporary(this, IDC_URL_ISTEMPORARY), m_urlAutoSend(this, IDC_URL_AUTOSEND), m_urlPasteToMessageInputArea(this, IDC_URL_COPYTOMIA), m_urlCopyToClipboard(this, IDC_URL_COPYTOCB) { - CreateLink(m_useShortUrl, "UseSortLinks", DBVT_BYTE, 1); + CreateLink(m_urlIsTemporary, "UrlIsTemporary", DBVT_BYTE, 0); CreateLink(m_urlAutoSend, "UrlAutoSend", DBVT_BYTE, 1); CreateLink(m_urlPasteToMessageInputArea, "UrlPasteToMessageInputArea", DBVT_BYTE, 0); CreateLink(m_urlCopyToClipboard, "UrlCopyToClipboard", DBVT_BYTE, 0); diff --git a/plugins/Dropbox/src/dropbox_options.h b/plugins/Dropbox/src/dropbox_options.h index 643bcd5c42..9f5ffa1607 100644 --- a/plugins/Dropbox/src/dropbox_options.h +++ b/plugins/Dropbox/src/dropbox_options.h @@ -12,7 +12,7 @@ private: CCtrlButton m_authorize; CCtrlBase m_authStatus; - CCtrlCheck m_useShortUrl; + CCtrlCheck m_urlIsTemporary; CCtrlCheck m_urlAutoSend; CCtrlCheck m_urlPasteToMessageInputArea; CCtrlCheck m_urlCopyToClipboard; diff --git a/plugins/Dropbox/src/dropbox_transfers.cpp b/plugins/Dropbox/src/dropbox_transfers.cpp index 556bccbcfc..7f7bf2b1e7 100644 --- a/plugins/Dropbox/src/dropbox_transfers.cpp +++ b/plugins/Dropbox/src/dropbox_transfers.cpp @@ -7,14 +7,10 @@ char* CDropbox::UploadFile(const char *data, size_t size, char *path) UploadFileRequest request(token, encodedPath, data, size); NLHR_PTR response(request.Send(hNetlibConnection)); - HandleJsonResponseError(response); + JSONNode root = HandleJsonResponse(response); + JSONNode node = root.at("path_lower"); + mir_strcpy(path, node.as_string().c_str()); - 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; } @@ -24,12 +20,7 @@ void CDropbox::StartUploadSession(const char *data, size_t size, char *sessionId StartUploadSessionRequest request(token, data, size); NLHR_PTR response(request.Send(hNetlibConnection)); - HandleJsonResponseError(response); - - JSONNode root = JSONNode::parse(response->pData); - if (root.empty()) - return; - + JSONNode root = HandleJsonResponse(response); JSONNode node = root.at("session_id"); mir_strcpy(sessionId, node.as_string().c_str()); } @@ -40,7 +31,7 @@ void CDropbox::AppendToUploadSession(const char *data, size_t size, const char * AppendToUploadSessionRequest request(token, sessionId, offset, data, size); NLHR_PTR response(request.Send(hNetlibConnection)); - HandleJsonResponseError(response); + HandleJsonResponse(response); } char* CDropbox::FinishUploadSession(const char *data, size_t size, const char *sessionId, size_t offset, char *path) @@ -49,14 +40,10 @@ char* CDropbox::FinishUploadSession(const char *data, size_t size, const char *s FinishUploadSessionRequest request(token, sessionId, offset, path, data, size); NLHR_PTR response(request.Send(hNetlibConnection)); - HandleJsonResponseError(response); + JSONNode root = HandleJsonResponse(response); + JSONNode node = root.at("path_lower"); + mir_strcpy(path, node.as_string().c_str()); - 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; } @@ -70,23 +57,50 @@ void CDropbox::CreateFolder(const char *path) if (response->resultCode == HTTP_STATUS_FORBIDDEN) return; - HandleJsonResponseError(response); + HandleJsonResponse(response); } void CDropbox::CreateDownloadUrl(const char *path, char *url) { ptrA token(db_get_sa(NULL, MODULE, "TokenSecret")); - GetTemporaryLinkRequest request(token, path); - NLHR_PTR response(request.Send(hNetlibConnection)); + if (db_get_b(NULL, MODULE, "UrlIsTemporary", 0)) { + GetTemporaryLinkRequest request(token, path); + NLHR_PTR response(request.Send(hNetlibConnection)); - HandleJsonResponseError(response); + JSONNode root = HandleJsonResponse(response); + JSONNode link = root.at("link"); + mir_strcpy(url, link.as_string().c_str()); + return; + } + + CreateSharedLinkRequest shareRequest(token, path); + NLHR_PTR response(shareRequest.Send(hNetlibConnection)); + + HandleHttpResponse(response); JSONNode root = JSONNode::parse(response->pData); - if (root.empty()) + if (root.isnull()) + throw DropboxException(HttpStatusToText(HTTP_STATUS_ERROR)); + + JSONNode error = root.at("error"); + if (error.isnull()) { + JSONNode link = root.at("link"); + mir_strcpy(url, link.as_string().c_str()); return; + } + + json_string tag = error.at(".tag").as_string(); + if (tag != "shared_link_already_exists") + throw DropboxException(tag.c_str()); + + GetSharedLinkRequest getRequest(token, path); + response = getRequest.Send(hNetlibConnection); + + root = HandleJsonResponse(response); - JSONNode node = root.at("link"); - mir_strcpy(url, node.as_string().c_str()); + JSONNode links = root.at("links").as_array(); + JSONNode link = links[0u].at("url"); + mir_strcpy(url, link.as_string().c_str()); } UINT CDropbox::UploadToDropbox(void *owner, void *arg) diff --git a/plugins/Dropbox/src/dropbox_utils.cpp b/plugins/Dropbox/src/dropbox_utils.cpp index cd71d187f7..39b76a9f2e 100644 --- a/plugins/Dropbox/src/dropbox_utils.cpp +++ b/plugins/Dropbox/src/dropbox_utils.cpp @@ -57,29 +57,34 @@ char* CDropbox::HttpStatusToText(HTTP_STATUS status) return "Unknown error"; } -void CDropbox::HandleJsonResponseError(NETLIBHTTPREQUEST *response) +void CDropbox::HandleHttpResponse(NETLIBHTTPREQUEST *response) { if (response == NULL) throw DropboxException(HttpStatusToText(HTTP_STATUS_ERROR)); +} - if (response->resultCode == HTTP_STATUS_OK) - return; +JSONNode CDropbox::HandleJsonResponse(NETLIBHTTPREQUEST *response) +{ + HandleHttpResponse(response); - if (response->resultCode != HTTP_STATUS_CONFLICT) { + if (response->resultCode != HTTP_STATUS_OK && + response->resultCode != HTTP_STATUS_CONFLICT) { if (response->dataLength) throw DropboxException(response->pData); throw DropboxException(HttpStatusToText((HTTP_STATUS)response->resultCode)); } JSONNode root = JSONNode::parse(response->pData); - if (root.empty()) + if (root.isnull()) throw DropboxException(HttpStatusToText(HTTP_STATUS_ERROR)); - JSONNode error = root.at("error_summary"); - if (error.empty()) - return; + JSONNode error = root.at("error"); + if (!error.isnull()) { + json_string tag = error.at(".tag").as_string(); + throw DropboxException(tag.c_str()); + } - throw DropboxException(error.as_string().c_str()); + return root; } MEVENT CDropbox::AddEventToDb(MCONTACT hContact, WORD type, DWORD flags, DWORD cbBlob, PBYTE pBlob) diff --git a/plugins/Dropbox/src/resource.h b/plugins/Dropbox/src/resource.h index a01728b502..1c8d33bf53 100644 --- a/plugins/Dropbox/src/resource.h +++ b/plugins/Dropbox/src/resource.h @@ -1,6 +1,6 @@ //{{NO_DEPENDENCIES}} // Microsoft Visual C++ generated include file. -// Used by d:\Projects\MirandaNG\plugins\Dropbox\res\resource.rc +// Used by C:\Users\unsane\Projects\c++\miranda-ng\plugins\Dropbox\res\resource.rc // #define IDI_DROPBOX 102 #define IDI_UPLOAD 103 @@ -8,9 +8,9 @@ #define IDD_OPTIONS_INTERCEPTION 110 #define IDC_REQUEST_CODE 1001 #define IDC_AUTHORIZE 1002 -#define IDC_CHECK2 1004 #define IDC_USE_SHORT_LINKS 1004 #define IDC_URL_USE_SHORT 1004 +#define IDC_URL_ISTEMPORARY 1004 #define IDC_AUTH_STATUS 1005 #define IDC_GET_AUTH_LINK 1006 #define IDC_URL_COPYTOCB 1009 @@ -26,7 +26,7 @@ // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 104 +#define _APS_NEXT_RESOURCE_VALUE 105 #define _APS_NEXT_COMMAND_VALUE 40001 #define _APS_NEXT_CONTROL_VALUE 1013 #define _APS_NEXT_SYMED_VALUE 101 diff --git a/plugins/Dropbox/src/version.h b/plugins/Dropbox/src/version.h index 95209b1064..8068f79a2b 100644 --- a/plugins/Dropbox/src/version.h +++ b/plugins/Dropbox/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 12 #define __RELEASE_NUM 2 -#define __BUILD_NUM 3 +#define __BUILD_NUM 4 #include -- cgit v1.2.3