unit ServerMethodsUnit1;

interface

uses System.SysUtils, System.Classes, System.Json,
    Datasnap.DSServer, Datasnap.DSAuth, DataSnap.DSProviderDataModuleAdapter, Data.DBXMySQL,
  Datasnap.Provider, Data.DB, Datasnap.DBClient, SimpleDS, Data.SqlExpr,vcl.Graphics;

type



  TServerMethods1 = class(TDSServerModule)
    SQLConnection1: TSQLConnection;
    SimpleDataSet1: TSimpleDataSet;
    DataSetProvider1: TDataSetProvider;
  private
    { Private declarations }
  public
    { Public declarations }

    procedure ZeroGlobalBuffer;
    procedure GlobalBufferToMySQL;
    procedure GetstreamByPackets(stream:tstream; packetsize:int64);

    procedure ApplyUpdates;

  end;





  var Globalbuffer:TBytes; //Массив байт для получения файлов, изображений...


implementation


{$R *.dfm}


uses System.StrUtils;

procedure TServerMethods1.ApplyUpdates;
begin
  simpledataset1.ApplyUpdates(-1);
end;



procedure TServerMethods1.GetstreamByPackets(stream: tstream;PacketSize:int64);
var ms:tmemorystream;  bmp:tbitmap; bufferstream:tmemorystream;
    localbuffer:tbytes;

begin


     ms:=tmemorystream.Create;
     ms.LoadFromStream(stream);
     ms.Position:=0;


     setlength(localbuffer,PacketSize) ;
     ms.ReadBuffer(localbuffer,PacketSize);

     globalbuffer:=globalbuffer+localbuffer;


     localbuffer:=nil;
     ms.Free;


end;


procedure TServerMethods1.GlobalBufferToMySQL;
var ms:tmemorystream;
begin
  if globalbuffer=nil then exit;  // Вышли, если нет данных

  ms:=tmemorystream.Create;
  ms.WriteBuffer(GlobalBuffer,length(Globalbuffer));

   simpledataset1.insert;
   simpledataset1.fieldbyname('Primary_key').AsInteger:=simpledataset1.RecordCount+1;
   tblobfield( simpledataset1.fieldbyname('Picture')).LoadFromStream(ms);
   simpledataset1.post;
   simpledataset1.applyUpdates(-1);

   ms.Free;
end;



procedure TServerMethods1.ZeroGlobalBuffer;
begin
 if GlobalBuffer<>nil then GlobalBuffer:=nil;
end;

end.

