unit srvTransfereciaFicheros_Impl;

{----------------------------------------------------------------------------}
{ This unit was automatically generated by the RemObjects SDK after reading  }
{ the RODL file associated with this project .                               }
{                                                                            }
{ This is where you are supposed to code the implementation of your objects. }
{----------------------------------------------------------------------------}

interface

uses
  {vcl:} Classes, SysUtils, 
  {RemObjects:} uROClientIntf, uROTypes, uROServer, uROServerIntf, uROSessions,
  {Required:} uRORemoteDataModule,
  {Used RODLs:} DataAbstract_Intf,
  {Generated:} VARELA_Intf;

type
  { TsrvTransfereciaFicheros }
  TsrvTransfereciaFicheros = class(TRORemoteDataModule, IsrvTransfereciaFicheros)
  private
    function getFileDirectory: String;
  protected
    { IsrvTransfereciaFicheros methods }
    procedure DownloadSequence(const Filename: String; const Sequence: Integer; out Filedata: Binary; out Filesize: Int64);
    procedure UploadChunk(const IsFirst: Boolean; const Filename: String; const Filedata: Binary);
    procedure UploadFinished(const Filename: String; const Filesize: Int64);
  end;

implementation

{$R *.dfm}
uses
  {Generated:} VARELA_Invk, uDataModuleServer,
  JclFileUtils;

procedure Create_srvTransfereciaFicheros(out anInstance : IUnknown);
begin
  anInstance := TsrvTransfereciaFicheros.Create(NIL);
end;

{ srvTransfereciaFicheros }
procedure TsrvTransfereciaFicheros.DownloadSequence(const Filename: String; const Sequence: Integer; out Filedata: Binary; out Filesize: Int64);
const
  Block: Integer = 65536;
var
  Position: Int64;
  MemStream: TFileStream;
  localfilename:String;
begin
  fileData := Binary.Create;
  localfilename:=getFileDirectory+filename;
  if not FileExists(localfilename) then exit;
  MemStream:=TFileStream.Create(localfilename,fmopenRead);
  try
   FileSize := MemStream.Size;
   Position := Block * (Sequence - 1);
   if Position <= FileSize then begin
      MemStream.Position := Position;
      if Position + Block > FileSize then
        fileData.CopyFrom(MemStream, FileSize - Position)
      else
        fileData.CopyFrom(MemStream, Block);
    end;
  finally
   MemStream.Free;
  end;
end;

function TsrvTransfereciaFicheros.getFileDirectory: String;
begin
  result := PathAddSeparator(dmServer.FDirEntrada);
end;

procedure TsrvTransfereciaFicheros.UploadChunk(const IsFirst: Boolean; const Filename: String; const Filedata: Binary);
var
  NewFile: TFileStream;
  localfilename:String;
begin
  Localfilename:= GetFileDirectory + Filename;
  if isfirst and Fileexists(localfilename) then DeleteFile(localfilename);
  if FileExists(localfilename)
      then NewFile := TFileStream.Create(localfilename,fmOpenReadWrite)
      else NewFile := TFileStream.Create(localfilename,fmCreate);
  try
    NewFile.Seek(0,soFromEnd);
    filedata.SaveToStream(NewFile);
  finally
    NewFile.Free;
  end;
end;

procedure TsrvTransfereciaFicheros.UploadFinished(const Filename: String; const Filesize: Int64);
begin
//  (EventRepository as IFileEvents_Writer).OnNewFileAvailable(Session.SessionID, Filename, Filesize);
end;

initialization
  TROClassFactory.Create('srvTransfereciaFicheros', Create_srvTransfereciaFicheros, TsrvTransfereciaFicheros_Invoker);

finalization

end.
