From 595b5085462a12dd7d9d5170afa883062df220c3 Mon Sep 17 00:00:00 2001 From: Alexander Pitkin Date: Sat, 28 Sep 2013 23:11:00 +0300 Subject: [PATCH] Added FsRenMovFileW plugin method Added method to copy/move Dropbox files Added copy and move DropboxClient methods --- DropboxAPI/DropboxClient.pas | 46 +++++++++++++++++++++++++++++++++ TCBox.dpr | 49 +++++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/DropboxAPI/DropboxClient.pas b/DropboxAPI/DropboxClient.pas index 83e4d55..e98fb06 100644 --- a/DropboxAPI/DropboxClient.pas +++ b/DropboxAPI/DropboxClient.pas @@ -21,6 +21,10 @@ TDropboxClient = class function metaData(path: string; list: boolean;file_limit: integer = 10000; hash:boolean=False;revision:string='';include_deleted:boolean=False):TJsonObject; // Return True if path refers to an existing path function exists(path: string): boolean; + // Copies a file or folder to a new location + function copy(fromPath: string; toPath: string): TJSONObject; + // Moves a file or folder to a new location + function move(fromPath: string; toPath: string): TJSONObject; // Return True if path is existsting file function isFile(path: string): boolean; // Return True if path is existsting directory @@ -106,6 +110,27 @@ function TDropboxClient.accountInfo(): TJsonObject; Result := FrestClient.GET_JSON(url); end; +function TDropboxClient.copy(fromPath, toPath: string): TJSONObject; +var + params, requestparams, requestheaders: TStringList; + url: string; +begin + try + params := TStringList.Create; + requestparams := TStringList.Create; + requestheaders := TStringList.Create; + params.Values['root'] := FSession.Root; + params.Values['from_path'] := fromPath; + params.Values['to_path'] := toPath; + url := request('/fileops/copy',requestparams,requestheaders,params,'POST'); + Result := FRestClient.POST_JSON(url, requestparams); + finally + params.Free; + requestparams.Free; + requestheaders.Free; + end; +end; + constructor TDropboxClient.Create(session: TDropboxSession); begin FSession := session; @@ -278,6 +303,27 @@ function TDropboxClient.metaData(path: string; list: boolean;file_limit: integer end; +function TDropboxClient.move(fromPath, toPath: string): TJSONObject; +var + params, requestparams, requestheaders: TStringList; + url: string; +begin + try + params := TStringList.Create; + requestparams := TStringList.Create; + requestheaders := TStringList.Create; + params.Values['root'] := FSession.Root; + params.Values['from_path'] := fromPath; + params.Values['to_path'] := toPath; + url := request('/fileops/move',requestparams,requestheaders,params,'POST'); + Result := FRestClient.POST_JSON(url, requestparams); + finally + params.Free; + requestparams.Free; + requestheaders.Free; + end; +end; + function TDropboxClient.parseDate(dateStr: string): TDateTime; const _ShortMonthNames : array[1..12] of string = ('Jan','Feb','Mar','Apr','May','Jun', diff --git a/TCBox.dpr b/TCBox.dpr index 5696bb9..df89700 100644 --- a/TCBox.dpr +++ b/TCBox.dpr @@ -1,4 +1,4 @@ -library TCBox; +library TCBox; uses Windows, @@ -590,6 +590,52 @@ begin end; end; +function FsRenMovFileW(OldName, NewName: pwidechar; Move, OverWrite: bool; + RemoteInfo: pRemoteInfo): Integer; stdcall; +var + oldFileName, newFileName: string; + newFileExists: boolean; + json: TJSONObject; +begin + oldFileName := normalizeDropboxPath(OldName); + newFileName := normalizeDropboxPath(NewName); + newFileExists := DropboxClient.exists(newFileName); + if not OverWrite and newFileExists then + begin + Result := FS_FILE_EXISTS; + exit; + end; + if OverWrite and newFileExists then + try + DropboxClient.delete(newFileName); + except + Result := FS_FILE_NOTSUPPORTED; + exit; + end; + try + if Move then + begin + // move object + json := DropboxClient.Move(oldFileName, newFileName); + json.Free; + end + else + begin + // copy objects + json := DropboxClient.copy(oldFileName, newFileName); + json.Free; + end; + Result := FS_FILE_OK; + except + on E: Exception do + begin + Log('Exception in FsRenMovFileW ' + E.ClassName + ' ' + E.Message); + Result := FS_FILE_WRITEERROR; + exit; + end; + end; +end; + procedure FsGetDefRootName(DefRootName: PAnsiChar; maxlen: Integer); stdcall; const rootName: String = 'Dropbox'; @@ -610,6 +656,7 @@ exports FsGetFileW, FsMkDirW, FsRemoveDirW, + FsRenMovFileW, FsDeleteFileW, FsPutFileW, FsGetDefRootName,