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,Dialogs
  ,Variants;

type



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

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

    procedure ApplyUpdates;
    procedure Hello (s:string);
    procedure DataInsert(V:Olevariant);

  end;


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


implementation


{$R *.dfm}


uses System.StrUtils;

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



function UnPackVarArray(V : Variant) : AnsiString;
var pData : PByteArray;
    S : String;
begin
  if V = NULL then begin
    Result := ''; Exit;
  end;
  pData := VarArrayLock(V);
  try
    SetLength(S, VarArrayHighBound(V, 1)+1);
    Move(pData^, S[1], Length(S));
  finally
    VarArrayUnlock(V);
  end;
  Result := S;
end;




procedure TServerMethods1.DataInsert(V: Olevariant);

var s:string; ss:tstringstream; ms:tmemorystream;
begin
s:=UnPackVarArray(V);
ss:=tstringstream.Create(s);
ss.Position:=0;
ms:=tmemorystream.Create;
ms.LoadFromStream(ss);


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



end;

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

begin

//showmessage('Поток на сервере ' +inttostr(stream.Size));
//if stream.Size=0 then exit;


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


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

     globalbuffer:=globalbuffer+localbuffer;


     setlength(localbuffer,0) ;
     ms.Free;

  //   stream.Clear;
 //    stream.Position:=0;
 //    stream:=tmemorystream.Create;

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.Hello(s:string);
var ss:tstringstream; ms:tmemorystream;
begin

   ss:=tstringstream.create(s);
//  showmessage('Строка на сервере '+inttostr(length(ss.DataString)));

   ms:=tmemorystream.create;

   ms.LoadFromStream(ss);


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

 //  if ms.Size>0 then
  // showmessage('hello');


end;

procedure TServerMethods1.ZeroGlobalBuffer;
begin
 if GlobalBuffer<>nil then GlobalBuffer:=nil;
 Setlength(Globalbuffer,0);
end;

end.

