BadScript 2
Loading...
Searching...
No Matches
BadFileSystemStackExtensions.cs
Go to the documentation of this file.
1using System.IO;
2using System.Net.Http;
3using System.Threading.Tasks;
4using BadScript2.IO;
6using Newtonsoft.Json.Linq;
7
9{
10 public static class BadFileSystemStackExtensions
11 {
12
13 public static BadFileSystemStack FromZip(this BadFileSystemStack stack, string file, string root = "/", string? name = null)
14 {
15 return stack.ConfigureLayer(() =>
16 {
17 var fs = new BadVirtualFileSystem();
18 fs.ImportZip(file, root);
19 return BadLayeredFileSystemLayer.Create(name ?? $"Zip({file})", fs, new JObject
20 {
21 ["File"] = file,
22 ["Root"] = root
23 });
24 });
25 }
26 public static BadFileSystemStack FromZip(this BadFileSystemStack stack, Stream stream, string root = "/", string? name = null)
27 {
28 return stack.ConfigureLayer(() =>
29 {
30 var fs = new BadVirtualFileSystem();
31 fs.ImportZip(stream, root);
32 return BadLayeredFileSystemLayer.Create(name ?? $"Zip(from Stream)", fs, new JObject
33 {
34 ["File"] = null,
35 ["Root"] = root
36 });
37 });
38 }
39
40 public static BadFileSystemStack FromDirectory(this BadFileSystemStack stack, string dir, string root = "/", string? name = null)
41 {
42 return stack.ConfigureLayer(() =>
43 {
44 var fs = new BadVirtualFileSystem();
45 fs.SetCurrentDirectory(root);
46
47 var fp = Path.GetFullPath(dir);
48 if (!fp.EndsWith(Path.DirectorySeparatorChar))
49 {
50 fp += Path.DirectorySeparatorChar;
51 }
52
53 foreach (var file in Directory.GetFiles(fp, "*", SearchOption.AllDirectories))
54 {
55 var relFile = file.Replace(fp, "");
56 using var s = File.OpenRead(file);
57 using var d = fs.OpenWrite(relFile, BadWriteMode.CreateNew);
58 s.CopyTo(d);
59 }
60
61 return BadLayeredFileSystemLayer.Create(name ?? $"Directory({fp})", fs, new JObject
62 {
63 ["Directory"] = fp,
64 ["Root"] = root
65 });
66 });
67 }
68
69 public static async Task<BadFileSystemStack> FromWebArchive(this BadFileSystemStack stack, string url, string root = "/", string? name = null)
70 {
71 using var http = new HttpClient();
72 return stack.FromZip(await http.GetStreamAsync(url), root, name ?? $"WebArchive({url})");
73 }
74 }
75}
static BadFileSystemStack FromDirectory(this BadFileSystemStack stack, string dir, string root="/", string? name=null)
static BadFileSystemStack FromZip(this BadFileSystemStack stack, Stream stream, string root="/", string? name=null)
static BadFileSystemStack FromZip(this BadFileSystemStack stack, string file, string root="/", string? name=null)
static async Task< BadFileSystemStack > FromWebArchive(this BadFileSystemStack stack, string url, string root="/", string? name=null)
BadFileSystemStack ConfigureLayer(Func< BadLayeredFileSystemLayer > config)
static BadLayeredFileSystemLayer Create(string name, BadVirtualFileSystem fs, JObject? metaData=null)
Virtual File System Implementation for the BadScript Engine.
Contains the Implementation of the BadScript Virtual File System.
Contains IO Implementation for the BadScript2 Runtime.
BadWriteMode
The Write Modes of the File System Abstraction.