Skip to content

Commit

Permalink
Рефакторинг копирования файла
Browse files Browse the repository at this point in the history
  • Loading branch information
pozitronik committed May 6, 2016
1 parent 42697cd commit c3e7ef7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions CloudMailRu.pas
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ TCloudMailRu = class
function HTTPPost(URL: WideString; PostData: TStringStream; var Answer: WideString; ContentType: WideString = 'application/x-www-form-urlencoded'): boolean; // Постинг данных с возможным получением ответа.

function HTTPPostFile(URL: WideString; PostData: TIdMultipartFormDataStream; var Answer: WideString): integer; // Постинг файла и получение ответа
function HTTPGetFile(URL: WideString; var FileStream: TMemoryStream): integer;
function HTTPGet(URL: WideString; var Answer: WideString): boolean;
function getTokenFromText(Text: WideString): WideString;
function get_x_page_id_FromText(Text: WideString): WideString;
Expand Down Expand Up @@ -308,9 +309,7 @@ function TCloudMailRu.HTTPPost(URL: WideString; PostData: TStringStream; var Ans
SSL: TIdSSLIOHandlerSocketOpenSSL;
begin
Result := true;

try

MemStream := TStringStream.Create;
self.HTTPInit(HTTP, SSL, self.Cookie);
if ContentType <> '' then HTTP.Request.ContentType := ContentType;
Expand All @@ -333,7 +332,6 @@ function TCloudMailRu.HTTPPost(URL: WideString; PostData: TStringStream; var Ans
Result := false;
end;
end;

end;
MemStream.Free;
end;
Expand All @@ -345,7 +343,6 @@ function TCloudMailRu.HTTPPostFile(URL: WideString; PostData: TIdMultipartFormDa
SSL: TIdSSLIOHandlerSocketOpenSSL;
begin
Result := CLOUD_OPERATION_OK;

try
self.HTTPInit(HTTP, SSL, self.Cookie);
MemStream := TStringStream.Create;
Expand All @@ -370,7 +367,6 @@ function TCloudMailRu.HTTPPostFile(URL: WideString; PostData: TIdMultipartFormDa
end;
end;
MemStream.Free

end;

function TCloudMailRu.HTTPGet(URL: WideString; var Answer: WideString): boolean;
Expand All @@ -392,11 +388,35 @@ function TCloudMailRu.HTTPGet(URL: WideString; var Answer: WideString): boolean;
Result := Answer <> '';
end;

function TCloudMailRu.HTTPGetFile(URL: WideString; var FileStream: TMemoryStream): integer;
var
HTTP: TIdHTTP;
SSL: TIdSSLIOHandlerSocketOpenSSL;
begin
Result := FS_FILE_OK;
try
self.HTTPInit(HTTP, SSL, self.Cookie);
HTTP.OnWork := self.HttpProgress;
HTTP.Get(URL, FileStream);
self.HTTPDestroy(HTTP, SSL);
except
on E: Exception do
begin
if E.ClassName = 'EAbort' then
begin
Result := FS_FILE_USERABORT;
end else begin
self.ExternalLogProc(ExternalPluginNr, MSGTYPE_IMPORTANTERROR, PWideChar(E.ClassName + ' ошибка с сообщением : ' + E.Message + ' при копировании файла с адреса ' + URL));
Result := FS_FILE_READERROR;
end;
end;
end;
end;

procedure TCloudMailRu.HTTPInit(var HTTP: TIdHTTP; var SSL: TIdSSLIOHandlerSocketOpenSSL; var Cookie: TIdCookieManager);
begin
SSL := TIdSSLIOHandlerSocketOpenSSL.Create();
if not(Assigned(Cookie)) then Cookie := TIdCookieManager.Create();

// if not(Assigned(Cookie)) then Cookie := TIdCookieManager.Create(); // Кука должна быть
HTTP := TIdHTTP.Create();
HTTP.CookieManager := Cookie;
HTTP.IOHandler := SSL;
Expand Down Expand Up @@ -470,7 +490,7 @@ function TCloudMailRu.getDir(path: WideString; var DirListing: TCloudMailRuDirLi
except
on E: Exception do
begin
self.ExternalLogProc(ExternalPluginNr, MSGTYPE_IMPORTANTERROR, PWideChar('Directory list getting error ' + E.Message));
self.ExternalLogProc(ExternalPluginNr, MSGTYPE_IMPORTANTERROR, PWideChar('Directory list receiving error ' + E.Message));
end;
end;
if not Result then exit(false);
Expand All @@ -480,8 +500,6 @@ function TCloudMailRu.getDir(path: WideString; var DirListing: TCloudMailRuDirLi
function TCloudMailRu.getFile(remotePath, localPath: WideString): integer; // 0 - ok, else error
var
FileStream: TMemoryStream;
HTTP: TIdHTTP;
SSL: TIdSSLIOHandlerSocketOpenSSL;
begin
if self.Shard = '' then
begin
Expand All @@ -497,29 +515,19 @@ function TCloudMailRu.getFile(remotePath, localPath: WideString): integer; // 0
end;

Result := FS_FILE_OK;
FileStream := TMemoryStream.Create;

remotePath := UrlEncode(StringReplace(remotePath, WideString('\'), WideString('/'), [rfReplaceAll, rfIgnoreCase]));

try
self.HTTPInit(HTTP, SSL, self.Cookie);
HTTP.OnWork := self.HttpProgress;
HTTP.Get(self.Shard + remotePath, FileStream); { TODO : В отдельную процедуру }
self.HTTPDestroy(HTTP, SSL);
FileStream := TMemoryStream.Create;
Result := self.HTTPGetFile(self.Shard + remotePath, FileStream);
if Result = FS_FILE_OK then FileStream.SaveToFile(localPath);
except
on E: Exception do
begin
if E.ClassName = 'EAbort' then
begin
Result := FS_FILE_USERABORT;
end else begin
self.ExternalLogProc(ExternalPluginNr, MSGTYPE_IMPORTANTERROR, PWideChar(E.ClassName + ' ошибка с сообщением : ' + E.Message));
Result := FS_FILE_READERROR;
end;

self.ExternalLogProc(ExternalPluginNr, MSGTYPE_IMPORTANTERROR, PWideChar('File receiving error ' + E.Message));
end;
end;
FileStream.SaveToFile(localPath);
FileStream.Free;
end;

function TCloudMailRu.publishFile(path: WideString; var PublicLink: WideString; publish: boolean = CLOUD_PUBLISH): boolean;
Expand Down
Binary file modified MailRuCloud.wfx
Binary file not shown.

0 comments on commit c3e7ef7

Please sign in to comment.