unit uDirectories_TPS_ChunkUploader;
{< will create dirs on client and Server for chunk Uploading}

interface

uses
  System.SysUtils, System.Classes, System.IOUtils, Vcl.Forms,

  // myUnits
  uCreateUniqueName_TPS_ChunkUploader
  ;

type
  TDirectories_TPS_ChunkUploader = class(TDataModule)
  private
    { Private declarations }

    FOutputDirOnServer:string;
    FTempDirOnServer:string;

    FTempDirOnClient:string;

    FOriginFileName:string;
    FUniqueNameFromOriginName:string;







  public
    { Public declarations }

    constructor Create(AOwner: TComponent); override;

    function CreateOutputDirOnServer: string;
    {< Relative output dir on Server}
    function CreateTempDirOnServer: string;
    {< Relative temp dir on Server}
    function CreateTempDirOnClient: string;

    procedure CreateUniqueNameFromOriginName();

    property OutputDirOnServer:String read FOutputDirOnServer write FOutputDirOnServer;
    property TempDirOnServer:string read FTempDirOnServer write FTempDirOnServer;
    property TempDirOnClient:string read FTempDirOnClient write FTempDirOnClient;

    property OriginFileName:string read FOriginFileName write FOriginFileName;
    property UniqueNameFromOriginName:string read FUniqueNameFromOriginName write FUniqueNameFromOriginName;



  end;

//var
//  Directories_TPS_ChunkUploader: TDirectories_TPS_ChunkUploader;

implementation

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}


uses uVisualFrame_TPS_ChunkUploader;
var VisualFrame_TPS_ChunkUploader:TVisualFrame_TPS_ChunkUploader;


{ TCreateDirs_TPS_ChunkUploader }

constructor TDirectories_TPS_ChunkUploader.Create(AOwner: TComponent);
begin
  inherited;

  if (AOwner is TVisualFrame_TPS_ChunkUploader) then
     VisualFrame_TPS_ChunkUploader:=(AOwner as TVisualFrame_TPS_ChunkUploader);

end;

function TDirectories_TPS_ChunkUploader.CreateOutputDirOnServer: string;

var
  Year: Word;
  Month: Word;
  Day: Word;

begin

Result:='';

  DecodeDate(Now, Year, Month, Day);

  with VisualFrame_TPS_ChunkUploader do
  begin

    FOutputDirOnServer := 'public' +'\'+
    'files' + '\' +
    HTTP_ConnectionParams.IDUser + '\' +
    Year.ToString + '\' +
    Month.ToString + '\' +
    Day.ToString;


    HTTPRequests.CreateDirOnServer(FOutputDirOnServer);
    Result:=FOutputDirOnServer;

  end;



end;



procedure TDirectories_TPS_ChunkUploader.CreateUniqueNameFromOriginName();

begin

FUniqueNameFromOriginName:=
TCreateUniqueName_TPS_ChunkUploader.
CreateUniqueNameAddingGUID(FOriginFileName,15);

end;


function TDirectories_TPS_ChunkUploader.CreateTempDirOnServer(): string;
var

  Year: Word;
  Month: Word;
  Day: Word;

  dirTempServer: string;



begin

Result:='';


  DecodeDate(Now, Year, Month, Day);
//  Filename := ExtractFileName(AFilePath);

  with VisualFrame_TPS_ChunkUploader do
  begin
    dirTempServer := 'public' +'\'+
    'files' + '\' +
    //HTTP_ConnectionParams.User +
    //'_ID' +
    HTTP_ConnectionParams.IDUser + '\' +
    Year.ToString + '\' +
    Month.ToString + '\' +
    Day.ToString + '\' +
    'temp' + '\' +
    FUniqueNameFromOriginName+'\';


  HTTPRequests.CreateDirOnServer(dirTempServer);

  end;

  Result:=dirTempServer;

end;




function TDirectories_TPS_ChunkUploader.CreateTempDirOnClient():string;
var
  Year: Word;
  Month: Word;
  Day: Word;

  tempDirOnClient: string;

begin

Assert(FUniqueNameFromOriginName<>'','Please fill FUniqueNameFromOriginName');

Result:='';

  try

        DecodeDate(Now, Year, Month, Day);


        with VisualFrame_TPS_ChunkUploader do
        begin
          tempDirOnClient := ExtractFilePath(Application.Exename) +
          'files' + '\' +
          HTTP_ConnectionParams.IDUser + '\' +
          Year.ToString + '\' +
          Month.ToString + '\' +
          Day.ToString + '\' +
          'temp' + '\' +
          FUniqueNameFromOriginName +'\';

        end;





      if not TDirectory.Exists(tempDirOnClient)
         then
         TDirectory.CreateDirectory(tempDirOnClient) else

         begin

           //deleting
            TDirectory.Delete(tempDirOnClient,true);
           //creating
            TDirectory.CreateDirectory(tempDirOnClient);


         end;


        Result:=tempDirOnClient;
        FTempDirOnClient:=tempDirOnClient;


  except

   on E:Exception do

    raise Exception.Create('Error creating temp directory on client EClassName'+E.ClassName+' '
    +'EMessage '+E.Message);

  end;


end;



end.
