forked from andersmelander/DWScriptStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamScript.Host.FileSystem.pas
More file actions
473 lines (375 loc) · 15.1 KB
/
amScript.Host.FileSystem.pas
File metadata and controls
473 lines (375 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
unit amScript.Host.FileSystem;
(*
* Copyright © 2012 Anders Melander
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*)
interface
uses
Classes,
SysUtils,
dwsFileSystem,
amScript.FileSystem.API,
amScript.FileSystem,
amScript.Host.API,
amScript.Provider.API;
// -----------------------------------------------------------------------------
//
// TScriptHostFileSystemFactory
//
// -----------------------------------------------------------------------------
// High level file system factory
// -----------------------------------------------------------------------------
type
TScriptHostFileSystemFactory = class(TScriptFileSystemFactory)
strict private
FDocument: IScriptHostDocument;
strict protected
public
constructor Create(const ADocument: IScriptHostDocument; AAllowProtected: boolean = False); reintroduce;
function AllocateFileSystem: IdwsFileSystem; override;
end;
// -----------------------------------------------------------------------------
//
// TScriptDocumentFileSystem
//
// -----------------------------------------------------------------------------
type
TScriptDocumentFileSystem = class(TScriptFileSystem)
strict private
FDocument: IScriptHostDocument;
strict protected
// IdwsFileSystem
function FileExists(const Filename: TFilename): boolean; override;
function OpenFileStream(const Filename: TFilename; const Mode: TdwsFileOpenMode): TStream; override;
// IScriptFileSystem
function OpenScriptProvider(const Filename: string; FileMustExist: boolean): IScriptProvider; override;
function ValidateFileNameEx(const FileName: string; FileMustExist: boolean): string; override;
public
constructor Create(const ADocument: IScriptHostDocument; const ARoot: string); reintroduce;
destructor Destroy; override;
class function IsAttachmentFilename(const Filename: string): boolean;
class function ExtractAttachmentFilename(const Filename: string): string;
class function ExtractAttachmentName(const Filename: string): string;
end;
// -----------------------------------------------------------------------------
//
// TScriptHostDelegatingFileSystem
//
// -----------------------------------------------------------------------------
type
TScriptHostDelegatingFileSystem = class(TScriptDelegatingFileSystem)
strict private
FDocument: IScriptHostDocument;
protected
public
constructor Create(const ADocument: IScriptHostDocument; const ARoot: string; ASearchPath: TStrings; AAllowProtected: boolean = False); reintroduce;
end;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
implementation
uses
StrUtils,
Windows,
RTLConsts,
dwsXPlatform, // must be before IOUtils to avoid TPath conflict
IOUtils,
amPath,
amEnvironment,
amScript.API,
amScript.Module,
amScript.Host.Provider,
amScript.Provider;
// -----------------------------------------------------------------------------
//
// TScriptDocumentFileSystem
//
// -----------------------------------------------------------------------------
constructor TScriptDocumentFileSystem.Create(const ADocument: IScriptHostDocument; const ARoot: string);
begin
inherited Create;
FDocument := ADocument;
end;
// -----------------------------------------------------------------------------
destructor TScriptDocumentFileSystem.Destroy;
begin
inherited;
end;
// -----------------------------------------------------------------------------
function TScriptDocumentFileSystem.FileExists(const Filename: TFilename): boolean;
var
ValidFilename: string;
ScriptHostFile: IScriptHostFile;
begin
Result := False;
if (FDocument = nil) then
exit;
ValidFilename := ValidateFileNameEx(Filename, True);
if (not IsAttachmentFilename(ValidFilename)) then
exit;
ValidFilename := ExtractAttachmentFilename(Filename);
Result := (FDocument.Files.TryGetFile(ValidFilename, ScriptHostFile)) or
(FDocument.Files.TryGetFile(ChangeFileExt(ValidFilename, ''), ScriptHostFile)); // Hack: Required to load .csv file. We need to handle this better
end;
// -----------------------------------------------------------------------------
class function TScriptDocumentFileSystem.ExtractAttachmentFilename(const Filename: string): string;
begin
Result := Filename;
if (Length(Result) > Length(sScriptAttachmentFilenamePrefix)) and ((sScriptAttachmentFilenamePrefix = '') or (Filename.StartsWith(sScriptAttachmentFilenamePrefix))) then
Delete(Result, 1, Length(sScriptAttachmentFilenamePrefix));
// if (AnsiSameText(ExtractFileExt(Result), sScriptFileType)) then // TODO : Why this?
// Result := ChangeFileExt(Result, '');
end;
class function TScriptDocumentFileSystem.ExtractAttachmentName(const Filename: string): string;
begin
Result := Filename;
if (Length(Result) > Length(sScriptAttachmentFilenamePrefix)) and (sScriptAttachmentFilenamePrefix <> '') and (Filename.StartsWith(sScriptAttachmentFilenamePrefix)) then
Delete(Result, 1, Length(sScriptAttachmentFilenamePrefix));
Result := ChangeFileExt(Result, '');
end;
class function TScriptDocumentFileSystem.IsAttachmentFilename(const Filename: string): boolean;
begin
Result := (Length(Filename) > Length(sScriptAttachmentFilenamePrefix)) and ((sScriptAttachmentFilenamePrefix = '') or (Filename.StartsWith(sScriptAttachmentFilenamePrefix)));
// Special handling of UNC filename: '\\name'
if (Result) and (sScriptAttachmentFilenamePrefix = '\') then
Result := (Filename[2] <> '\'); // We know that the length is at least 2 since we test for that above
end;
// -----------------------------------------------------------------------------
function TScriptDocumentFileSystem.OpenFileStream(const Filename: TFilename; const mode: TdwsFileOpenMode): TStream;
var
ValidFilename: string;
ScriptHostFile: IScriptHostFile;
begin
if (FDocument = nil) then
raise EScript.CreateFmt('Attachment not valid in this context: %s', [Filename]);
ValidFilename := ValidateFileNameEx(Filename, (Mode <> fomCreate));
if (ValidFilename = '') then
raise EScript.CreateFmt('Attachment not found: %s', [Filename]);
if (not IsAttachmentFilename(ValidFilename)) then
raise EScript.CreateFmt('Invalid Attachment name: %s', [Filename]);
ValidFilename := ExtractAttachmentName(Filename);
if (not FDocument.Files.TryGetFile(ValidFilename, ScriptHostFile)) then
begin
ValidFilename := ExtractAttachmentFilename(Filename);
if (not FDocument.Files.TryGetFile(ValidFilename, ScriptHostFile)) then // Hack: Required to load .csv file. We need to handle this better
raise EScript.CreateFmt('Attachment not found: %s', [Filename]);
end;
Result := TDocumentAttachmentStream.Create(ScriptHostFile);
end;
// -----------------------------------------------------------------------------
function TScriptDocumentFileSystem.OpenScriptProvider(const Filename: string; FileMustExist: boolean): IScriptProvider;
var
ValidFilename: string;
begin
ValidFilename := ValidateFileNameEx(Filename, True); // File must exist
// Must return nil if filename doesn't apply to this filesystem or delegating filesystem will not work
if (ValidFilename = '') then
Exit(nil);
// raise EScript.CreateFmt('Attachment not found: %s', [Filename]);
if (not IsAttachmentFilename(ValidFilename)) then
raise EScript.CreateFmt('Invalid Attachment name: %s', [Filename]);
if (FDocument = nil) then
raise EScript.CreateFmt('Attachment not valid in this context: %s', [Filename]);
ValidFilename := ExtractAttachmentFilename(Filename);
Result := TDocumentScriptProvider.Create(FDocument, ValidFilename);
end;
// -----------------------------------------------------------------------------
function TScriptDocumentFileSystem.ValidateFileNameEx(const FileName: string; FileMustExist: boolean): string;
var
AttachmentName: string;
ScriptHostFile: IScriptHostFile;
begin
if (IsAttachmentFilename(Filename)) and (FDocument <> nil) then
begin
if (FileMustExist) then
begin
AttachmentName := ExtractAttachmentFilename(Filename);
if (FDocument.Files.TryGetFile(AttachmentName, ScriptHostFile)) or
(FDocument.Files.TryGetFile(ChangeFileExt(AttachmentName, ''), ScriptHostFile)) then // Hack: Required to load .csv file. We need to handle this better
Result := Filename
else
Result := ''
end else
Result := Filename;
end else
Result := '';
end;
// -----------------------------------------------------------------------------
//
// TScriptBundleFileSystem
//
// -----------------------------------------------------------------------------
{$ifdef FEATURE_SCRIPT_BUNDLE}
constructor TScriptBundleFileSystem.Create(const AFilename: string; AAllowProtected: boolean);
begin
inherited Create;
FFilename := AFilename;
FAllowProtected := AAllowProtected;
end;
destructor TScriptBundleFileSystem.Destroy;
begin
FZipFile.Free;
FZipFileStream.Free;
inherited;
end;
// -----------------------------------------------------------------------------
function TScriptBundleFileSystem.GetZipFile: TZipFile;
begin
if (FZipFile = nil) then
begin
// Must use TZipFile.Open(TFileStream) to prevent file from being locked
FZipFileStream := TFileStream.Create(FFilename, fmOpenRead or fmShareDenyWrite);
FZipFile := TZipFile.Create;
FZipFile.Open(FZipFileStream, zmRead);
end;
Result := FZipFile;
end;
// -----------------------------------------------------------------------------
function TScriptBundleFileSystem.FileExists(const Filename: string): boolean;
begin
Result := (ValidateFileNameEx(Filename, True) <> '');
end;
// -----------------------------------------------------------------------------
function TScriptBundleFileSystem.OpenFileStream(const Filename: string; const mode: TdwsFileOpenMode): TStream;
var
ValidFilename: string;
Index: integer;
LocalHeader: TZipHeader;
SourceStream: TMemoryStream;
Stream: TStream;
begin
if (not (Mode in [fomReadOnly, fomFastSequentialRead])) then
raise EScript.Create('Invalid file open mode');
ValidFilename := ValidateFileNameEx(Filename, True);
if (ValidFilename <> '') then
Index := ZipFileIndexOf(ZipFile, ValidFilename)
else
Index := -1;
if (Index = -1) then
raise EScript.CreateFmt('File not found in bundle: %s (%s)', [Filename, FFilename]);
if (FAllowProtected) and (AnsiSameText(Copy(ZipFile.FileComment[Index], 1, ZipFile.FileInfo[Index].FileCommentLength) , 'protected')) then
begin
Result := TMemoryStream.Create;
try
SourceStream := TMemoryStream.Create;
try
ZipFile.Read(Index, Stream, LocalHeader);
try
SourceStream.CopyFrom(Stream, LocalHeader.UncompressedSize);
SourceStream.Position := 0;
ScriptDecryptStream(SourceStream, Result);
//ScriptDecryptStream(Stream, Result);
finally
Stream.Free;
end;
Result.Position := 0;
finally
SourceStream.Free;
end;
except
Result.Free;
raise;
end;
end else
begin
Result := TMemoryStream.Create;
try
ZipFile.Read(Index, Stream, LocalHeader);
try
Result.CopyFrom(Stream, LocalHeader.UncompressedSize);
finally
Stream.Free;
end;
Result.Position := 0;
except
Result.Free;
raise;
end;
end;
end;
// -----------------------------------------------------------------------------
function TScriptBundleFileSystem.OpenScriptProvider(const Filename: string; FileMustExist: boolean): IScriptProvider;
var
ValidFilename: string;
Stream: TStream;
begin
ValidFilename := ValidateFileNameEx(Filename, True); // File must exist
// Must return nil if filename doesn't apply to this filesystem or delegating filesystem will not work
if (ValidFilename = '') then
Exit(nil);
// raise EScript.CreateFmt('File not found in zip: %s (%s)', [Filename, FFilename]);
Stream := OpenFileStream(ValidFilename, fomReadOnly);
Result := TBundleScriptProvider.Create(FFilename, ValidFilename, FAllowProtected, Stream);
end;
// -----------------------------------------------------------------------------
function TScriptBundleFileSystem.ValidateFileName(const Filename: string): string;
begin
Result := ValidateFileNameEx(Filename, True);
end;
// -----------------------------------------------------------------------------
function TScriptBundleFileSystem.ValidateFileNameEx(const FileName: string; FileMustExist: boolean): string;
var
ValidFilename: string;
Index: integer;
begin
Result := '';
// Make sure we have a valid Windows path (Zip can use either / or \)
ValidFilename := StringReplace(Filename, '/', '\', [rfReplaceAll]);
// Remove double slashes
ValidFilename := RemoveDoubleBackslashesFromPath(ValidFilename);
if (ValidFilename = '') then
exit;
if (not TPath.IsRelativePath(ValidFilename)) then
Exit;
// Remove leading \
if (ValidFilename[1] = '\') then
Delete(ValidFilename, 1, 1);
Index := ZipFileIndexOf(ZipFile, ValidFilename);
if (Index <> -1) then
begin
Result := ZipFile.FileName[Index];
if (not FAllowProtected) and (AnsiSameText(Copy(ZipFile.FileComment[Index], 1, ZipFile.FileInfo[Index].FileCommentLength) , 'protected')) then
Result := '';
end;
end;
{$endif FEATURE_SCRIPT_BUNDLE}
// -----------------------------------------------------------------------------
//
// TScriptHostDelegatingFileSystem
//
// -----------------------------------------------------------------------------
constructor TScriptHostDelegatingFileSystem.Create(const ADocument: IScriptHostDocument; const ARoot: string; ASearchPath: TStrings; AAllowProtected: boolean);
begin
inherited Create(ARoot, ASearchPath, AAllowProtected);
FDocument := ADocument;
if (FDocument <> nil) then
FileSystems.Add(TScriptDocumentFileSystem.Create(FDocument, ARoot));
end;
// -----------------------------------------------------------------------------
//
// TScriptHostFileSystemFactory
//
// -----------------------------------------------------------------------------
constructor TScriptHostFileSystemFactory.Create(const ADocument: IScriptHostDocument; AAllowProtected: boolean);
begin
inherited Create(AAllowProtected);
FDocument := ADocument;
end;
// -----------------------------------------------------------------------------
function TScriptHostFileSystemFactory.AllocateFileSystem: IdwsFileSystem;
var
i: integer;
begin
Result := TScriptHostDelegatingFileSystem.Create(FDocument, Root, SearchPath, AllowProtected);
if (RootFileSystem <> nil) then
TScriptHostDelegatingFileSystem(Result).MountFileSystem(RootFileSystem);
for i := 0 to FileSystems.Count-1 do
TScriptHostDelegatingFileSystem(Result).MountFileSystem(FileSystems[i]);
end;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
end.