UPD: MacCloud: step-58: extract common code to TCloudDriverListFolderSession / TCloudDriverDefaultLister

This commit is contained in:
rich2014 2025-04-16 15:31:04 +08:00
commit 8c50148b40
3 changed files with 132 additions and 170 deletions

View file

@ -20,21 +20,16 @@ type
{ TDropBoxListFolderSession }
TDropBoxListFolderSession = class
TDropBoxListFolderSession = class( TCloudDriverListFolderSession )
private
_authSession: TCloudDriverAuthPKCESession;
_path: String;
_files: TCloudFiles;
_cursor: String;
_hasMore: Boolean;
private
procedure listFolderFirst;
procedure listFolderContinue;
procedure analyseListResult( const jsonString: String );
protected
procedure listFolderFirst; override;
procedure listFolderContinue; override;
public
constructor Create( const authSession: TCloudDriverAuthPKCESession; const path: String );
destructor Destroy; override;
function getNextFile: TCloudFile;
constructor Create( const authSession: TCloudDriverAuthPKCESession; const path: String ); override;
end;
{ TDropBoxDownloadSession }
@ -112,19 +107,6 @@ type
procedure move;
end;
{ TDropBoxLister }
TDropBoxLister = class( TCloudDriverLister )
private
_listFolderSession: TDropBoxListFolderSession;
public
constructor Create( const authSession: TCloudDriverAuthPKCESession; const path: String );
destructor Destroy; override;
procedure listFolderBegin; override;
function listFolderGetNextFile: TCloudFile; override;
procedure listFolderEnd; override;
end;
{ TDropBoxClient }
TDropBoxClient = class( TCloudDriver )
@ -402,35 +384,14 @@ begin
end;
constructor TDropBoxListFolderSession.Create( const authSession: TCloudDriverAuthPKCESession; const path: String );
var
truePath: String;
begin
_authSession:= authSession;
if path <> '/' then
_path:= path;
_files:= TCloudFiles.Create;
end;
destructor TDropBoxListFolderSession.Destroy;
begin
FreeAndNil( _files );
end;
function TDropBoxListFolderSession.getNextFile: TCloudFile;
function popFirst: TCloudFile;
begin
if _files.Count > 0 then begin
Result:= TCloudFile( _files.First );
_files.Delete( 0 );
end else begin
Result:= nil;
end;
end;
begin
Result:= popFirst;
if (Result=nil) and _hasMore then begin
listFolderContinue;
Result:= popFirst;
end;
if path = PathDelim then
truePath:= EmptyStr
else
truePath:= path;
Inherited Create( authSession, truePath );
end;
{ TDropBoxDownloadSession }
@ -756,35 +717,6 @@ begin
copyOrMove( True );
end;
{ TDropBoxLister }
constructor TDropBoxLister.Create(
const authSession: TCloudDriverAuthPKCESession;
const path: String );
begin
_listFolderSession:= TDropBoxListFolderSession.Create( authSession, path );
end;
destructor TDropBoxLister.Destroy;
begin
FreeAndNil( _listFolderSession );
end;
procedure TDropBoxLister.listFolderBegin;
begin
_listFolderSession.listFolderFirst;
end;
function TDropBoxLister.listFolderGetNextFile: TCloudFile;
begin
Result:= _listFolderSession.getNextFile;
end;
procedure TDropBoxLister.listFolderEnd;
begin
self.Free;
end;
{ TDropBoxClient }
class function TDropBoxClient.driverName: String;
@ -843,7 +775,7 @@ end;
function TDropBoxClient.createLister( const path: String ): TCloudDriverLister;
begin
Result:= TDropBoxLister.Create( _authSession, path );
Result:= TCloudDriverDefaultLister.Create( TDropBoxListFolderSession, _authSession, path );
end;
procedure TDropBoxClient.download(

View file

@ -202,6 +202,41 @@ type
function getToken: TCloudDriverToken;
end;
{ TCloudDriverListFolderSession }
TCloudDriverListFolderSession = class
protected
_authSession: TCloudDriverAuthPKCESession;
_path: String;
_files: TCloudFiles;
_hasMore: Boolean;
protected
procedure listFolderFirst; virtual; abstract;
procedure listFolderContinue; virtual; abstract;
public
constructor Create( const authSession: TCloudDriverAuthPKCESession; const path: String ); virtual;
destructor Destroy; override;
function getNextFile: TCloudFile;
end;
TCloudDriverListFolderSessionClass = class of TCloudDriverListFolderSession;
{ TCloudDriverDefaultLister }
TCloudDriverDefaultLister = class( TCloudDriverLister )
private
_listFolderSession: TCloudDriverListFolderSession;
public
constructor Create(
const sessionClass: TCloudDriverListFolderSessionClass;
const authSession: TCloudDriverAuthPKCESession;
const path: String );
destructor Destroy; override;
procedure listFolderBegin; override;
function listFolderGetNextFile: TCloudFile; override;
procedure listFolderEnd; override;
end;
var
cloudDriverManager: TCloudDriverManager;
@ -272,6 +307,70 @@ begin
_accessExpirationTime:= 0;
end;
{ TCloudDriverListFolderSession }
constructor TCloudDriverListFolderSession.Create(
const authSession: TCloudDriverAuthPKCESession; const path: String);
begin
_authSession:= authSession;
_files:= TCloudFiles.Create;
_path:= path;
end;
destructor TCloudDriverListFolderSession.Destroy;
begin
FreeAndNil( _files );
end;
function TCloudDriverListFolderSession.getNextFile: TCloudFile;
function popFirst: TCloudFile;
begin
if _files.Count > 0 then begin
Result:= TCloudFile( _files.First );
_files.Delete( 0 );
end else begin
Result:= nil;
end;
end;
begin
Result:= popFirst;
if (Result=nil) and _hasMore then begin
listFolderContinue;
Result:= popFirst;
end;
end;
{ TCloudDriverDefaultLister }
constructor TCloudDriverDefaultLister.Create(
const sessionClass: TCloudDriverListFolderSessionClass;
const authSession: TCloudDriverAuthPKCESession;
const path: String);
begin
_listFolderSession:= sessionClass.Create( authSession, path );
end;
destructor TCloudDriverDefaultLister.Destroy;
begin
FreeAndNil( _listFolderSession );
end;
procedure TCloudDriverDefaultLister.listFolderBegin;
begin
_listFolderSession.listFolderFirst;
end;
function TCloudDriverDefaultLister.listFolderGetNextFile: TCloudFile;
begin
Result:= _listFolderSession.getNextFile;
end;
procedure TCloudDriverDefaultLister.listFolderEnd;
begin
self.Free;
end;
{ TCloudDriverAuthPKCESession }
procedure TCloudDriverAuthPKCESession.requestAuthorization;

View file

@ -18,26 +18,21 @@ uses
type
{ TCloudDriverListFolderSession }
{ TYandexListFolderSession }
TCloudDriverListFolderSession = class
const
TYandexListFolderSession = class( TCloudDriverListFolderSession )
private const
LIMIT = 100;
private
_authSession: TCloudDriverAuthPKCESession;
_path: String;
_files: TCloudFiles;
_offset: Integer;
_total: Integer;
_hasMore: Boolean;
private
procedure listFolderFirst;
procedure listFolderContinue;
procedure analyseListResult( const jsonString: String );
protected
procedure listFolderFirst; override;
procedure listFolderContinue; override;
public
constructor Create( const authSession: TCloudDriverAuthPKCESession; const path: String );
destructor Destroy; override;
function getNextFile: TCloudFile;
constructor Create( const authSession: TCloudDriverAuthPKCESession; const path: String ); override;
end;
{ TYandexDownloadSession }
@ -112,19 +107,6 @@ type
procedure move;
end;
{ TYandexLister }
TYandexLister = class( TCloudDriverLister )
private
_listFolderSession: TCloudDriverListFolderSession;
public
constructor Create( const authSession: TCloudDriverAuthPKCESession; const path: String );
destructor Destroy; override;
procedure listFolderBegin; override;
function listFolderGetNextFile: TCloudFile; override;
procedure listFolderEnd; override;
end;
{ TYandexClient }
TYandexClient = class( TCloudDriver )
@ -257,14 +239,14 @@ begin
end;
end;
{ TCloudDriverListFolderSession }
{ TYandexListFolderSession }
procedure TCloudDriverListFolderSession.listFolderFirst;
procedure TYandexListFolderSession.listFolderFirst;
begin
listFolderContinue;
end;
procedure TCloudDriverListFolderSession.listFolderContinue;
procedure TYandexListFolderSession.listFolderContinue;
var
http: TMiniHttpClient;
httpResult: TMiniHttpResult = nil;
@ -296,7 +278,7 @@ begin
end;
end;
procedure TCloudDriverListFolderSession.analyseListResult(const jsonString: String);
procedure TYandexListFolderSession.analyseListResult(const jsonString: String);
var
json: NSDictionary;
jsonEmbedded: NSDictionary;
@ -336,37 +318,15 @@ begin
_hasMore:= ( _offset < _total );
end;
constructor TCloudDriverListFolderSession.Create( const authSession: TCloudDriverAuthPKCESession; const path: String );
constructor TYandexListFolderSession.Create( const authSession: TCloudDriverAuthPKCESession; const path: String );
var
truePath: String;
begin
_authSession:= authSession;
_files:= TCloudFiles.Create;
_path:= path;
if _path = EmptyStr then
_path:= PathDelim;
end;
destructor TCloudDriverListFolderSession.Destroy;
begin
FreeAndNil( _files );
end;
function TCloudDriverListFolderSession.getNextFile: TCloudFile;
function popFirst: TCloudFile;
begin
if _files.Count > 0 then begin
Result:= TCloudFile( _files.First );
_files.Delete( 0 );
end else begin
Result:= nil;
end;
end;
begin
Result:= popFirst;
if (Result=nil) and _hasMore then begin
listFolderContinue;
Result:= popFirst;
end;
if path = EmptyStr then
truePath:= PathDelim
else
truePath:= path;
Inherited Create( authSession, truePath );
end;
{ TYandexDownloadSession }
@ -634,35 +594,6 @@ begin
copyOrMove( True );
end;
{ TYandexLister }
constructor TYandexLister.Create(
const authSession: TCloudDriverAuthPKCESession;
const path: String);
begin
_listFolderSession:= TCloudDriverListFolderSession.Create( authSession, path );
end;
destructor TYandexLister.Destroy;
begin
FreeAndNil( _listFolderSession );
end;
procedure TYandexLister.listFolderBegin;
begin
_listFolderSession.listFolderFirst;
end;
function TYandexLister.listFolderGetNextFile: TCloudFile;
begin
Result:= _listFolderSession.getNextFile;
end;
procedure TYandexLister.listFolderEnd;
begin
self.Free;
end;
{ TYandexClient }
class function TYandexClient.driverName: String;
@ -721,7 +652,7 @@ end;
function TYandexClient.createLister( const path: String ): TCloudDriverLister;
begin
Result:= TYandexLister.Create( _authSession, path );
Result:= TCloudDriverDefaultLister.Create( TYandexListFolderSession, _authSession, path );
end;
procedure TYandexClient.download(