unit ServerMethodsUnit1;

interface

uses System.SysUtils,Messages,Vcl.Dialogs, System.Classes, System.Json,vcl.Graphics,
    Datasnap.DSServer, Datasnap.DSAuth, DataSnap.DSProviderDataModuleAdapter, Data.DBXMySQL,
  Datasnap.Provider, Data.DB, Datasnap.DBClient, SimpleDS, Data.SqlExpr,Vcl.ExtCtrls,
  vcl.Forms, Data.FMTBcd;

type
  TServerMethods1 = class(TDSServerModule)
    SQLConnection1: TSQLConnection;
    SimpleDataSet1: TSimpleDataSet;
    DataSetProvider1: TDataSetProvider;
    procedure DataSetProvider1GetTableName(Sender: TObject; DataSet: TDataSet;
      var TableName: string);
    procedure DSServerModuleCreate(Sender: TObject);
    procedure DataSetProvider1BeforeGetRecords(Sender: TObject; var OwnerData: OleVariant);
    procedure DataSetProvider1AfterGetRecords(Sender: TObject; var OwnerData: OleVariant);
  private
    { Private declarations }
  public
    { Public declarations }
    function EchoString(Value: string): string;
    function ReverseString(Value: string): string;
    procedure GetPicture(filename:TmemoryStream);
    procedure GetString(s:widestring);
  end;

implementation


{$R *.dfm}


uses System.StrUtils;

procedure TServerMethods1.DataSetProvider1AfterGetRecords(Sender: TObject;
  var OwnerData: OleVariant);
begin
simpledataset1.Open;
end;

procedure TServerMethods1.DataSetProvider1BeforeGetRecords(Sender: TObject;
  var OwnerData: OleVariant);
begin
simpledataset1.Close;
end;

procedure TServerMethods1.DataSetProvider1GetTableName(Sender: TObject; DataSet: TDataSet;
  var TableName: string);
begin
TableName:='Pictures';
end;

procedure TServerMethods1.DSServerModuleCreate(Sender: TObject);
begin
simpledataset1.Open;
end;

function TServerMethods1.EchoString(Value: string): string;
begin
  Result := Value;
end;

procedure TServerMethods1.GetPicture(filename: Tmemorystream);
var ms1:tmemorystream; bmp:tbitmap; image:timage; f:tform;
begin
  bmp:=tbitmap.Create;
  ms1:=tmemorystream.Create;

  ms1.LoadFromStream(filename);
  showmessage(inttostr(filename.Size));

  showmessage(inttostr(ms1.Size));


//  bmp.LoadFromStream(ms);


{
f:=tform.Create(Self);
f.Width:=500;
f.Height:=500;
f.Caption:='Test picture';
f.show;

image:=timage.Create(Self);
image.Parent:=f;
image.SetBounds(0,0,500,500);
image.Picture.Graphic.LoadFromStream(ms);
}


  //  if ms<>nil then showmessage('not empty on server');


//  simpledataset1.Insert;
//  simpledataset1.FieldByName('Primary_key').AsInteger:=simpledataset1.RecordCount+1;
//     simpledataset1.FieldByName('Picture').Assign(bmp);
  //  Tblobfield(simpledataset1.FieldByName('Picture')).Assign(bmp);
//  simpledataset1.Post;
//  simpledataset1.ApplyUpdates(-1);

end;

procedure TServerMethods1.GetString(s: widestring);
var ss:Tstringstream; ms:tmemorystream; bmp:tbitmap;f:tform; image:timage;
begin
//showmessage(inttostr(length(s)));

ss:=tstringstream.Create(s);
bmp:=tbitmap.Create;

//ms.LoadFromStream(ss);
bmp.LoadFromStream(ss);

{
f:=tform.Create(Self);
f.Width:=500;
f.Height:=500;
f.Caption:='Test picture';
f.show;

image:=timage.Create(Self);
image.Parent:=f;
image.SetBounds(0,0,500,500);
image.Picture.Assign(bmp);
}


simpledataset1.Append;
simpledataset1.FieldByName('Primary_key').AsInteger:=simpledataset1.RecordCount+1;
TBlobfield(simpledataset1.FieldByName('Picture')).Assign(bmp);// AsString:=s;
simpledataset1.Post;
simpledataset1.ApplyUpdates(-1);

simpledataset1.Close;
simpledataset1.Open;

end;

function TServerMethods1.ReverseString(Value: string): string;
begin
  Result := System.StrUtils.ReverseString(Value);
end;

end.

