This repository was archived by the owner on Dec 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 840
Expand file tree
/
Copy pathlocalFileSystem.ts
More file actions
208 lines (179 loc) · 7.25 KB
/
localFileSystem.ts
File metadata and controls
208 lines (179 loc) · 7.25 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
import { BrowserWindow, dialog } from "electron";
import fs from "fs";
import path from "path";
import rimraf from "rimraf";
import { IStorageProvider } from "../../../providers/storage/storageProviderFactory";
import { IAsset, AssetType, StorageType, IConnection } from "../../../models/applicationState";
import { AssetService } from "../../../services/assetService";
import { strings } from "../../../common/strings";
import { ILocalFileSystemProxyOptions } from "../../../providers/storage/localFileSystemProxy";
export default class LocalFileSystem implements IStorageProvider {
public storageType: StorageType.Local;
constructor(private browserWindow: BrowserWindow) { }
public selectContainer(): Promise<string> {
return new Promise<string>((resolve, reject) => {
const filePaths = dialog.showOpenDialog(this.browserWindow, {
title: strings.connections.providers.local.selectFolder,
buttonLabel: strings.connections.providers.local.chooseFolder,
properties: ["openDirectory", "createDirectory"],
});
if (!filePaths || filePaths.length !== 1) {
return reject();
}
resolve(filePaths[0]);
});
}
public readText(filePath: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
fs.readFile(path.normalize(filePath), (err: NodeJS.ErrnoException, data: Buffer) => {
if (err) {
return reject(err);
}
resolve(data.toString());
});
});
}
public readBinary(filePath: string): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => {
fs.readFile(path.normalize(filePath), (err: NodeJS.ErrnoException, data: Buffer) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
}
public writeBinary(filePath: string, contents: Buffer): Promise<void> {
return new Promise<void>((resolve, reject) => {
const containerName: fs.PathLike = path.normalize(path.dirname(filePath));
const exists = fs.existsSync(containerName);
if (!exists) {
fs.mkdirSync(containerName);
}
fs.writeFile(path.normalize(filePath), contents, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
}
public writeText(filePath: string, contents: string): Promise<void> {
const buffer = Buffer.from(contents);
return this.writeBinary(filePath, buffer);
}
public deleteFile(filePath: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
const exists = fs.existsSync(path.normalize(filePath));
if (!exists) {
resolve();
}
fs.unlink(filePath, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
}
public async listFiles(folderPath: string): Promise<string[]> {
const normalizedPath = path.normalize(folderPath);
console.log(`Listing files from ${normalizedPath}`);
const files = await this.listItems(normalizedPath, (stats) => !stats.isDirectory());
const directories = await this.listItems(normalizedPath, (stats) => stats.isDirectory());
await Promise.all(directories.map(async (directory) => {
const directoryFiles = await this.listFiles(directory);
directoryFiles.forEach((file) => files.push(file));
}));
return files;
}
public listContainers(folderPath: string): Promise<string[]> {
return this.listItems(path.normalize(folderPath), (stats) => stats.isDirectory());
}
public createContainer(folderPath: string): Promise<void> {
return new Promise((resolve, reject) => {
fs.exists(path.normalize(folderPath), (exists) => {
if (exists) {
resolve();
} else {
fs.mkdir(path.normalize(folderPath), (err) => {
if (err) {
return reject(err);
}
resolve();
});
}
});
});
}
public deleteContainer(folderPath: string): Promise<void> {
return new Promise((resolve, reject) => {
fs.exists(path.normalize(folderPath), (exists) => {
if (exists) {
rimraf(path.normalize(folderPath), (err) => {
if (err) {
return reject(err);
}
resolve();
});
} else {
resolve();
}
});
});
}
public async getAssets(sourceConnectionFolderPath?: string, relativePath: boolean = false): Promise<IAsset[]> {
const files = await this.listFiles(path.normalize(sourceConnectionFolderPath));
return files.map((filePath) => AssetService.createAssetFromFilePath(
filePath,
undefined,
relativePath ? path.relative(sourceConnectionFolderPath, filePath) : filePath))
.filter((asset) => asset.type !== AssetType.Unknown);
}
/**
* Gets a list of file system items matching the specified predicate within the folderPath
* @param {string} folderPath
* @param {(stats:fs.Stats)=>boolean} predicate
* @returns {Promise} Resolved list of matching file system items
*/
private listItems(folderPath: string, predicate: (stats: fs.Stats) => boolean) {
return new Promise<string[]>((resolve, reject) => {
fs.readdir(path.normalize(folderPath), async (err: NodeJS.ErrnoException, fileSystemItems: string[]) => {
if (err) {
return reject(err);
}
const getStatsTasks = fileSystemItems.map((name) => {
const filePath = path.join(folderPath, name);
return this.getStats(filePath);
});
try {
const statsResults = await Promise.all(getStatsTasks);
const filteredItems = statsResults
.filter((result) => predicate(result.stats))
.map((result) => result.path);
resolve(filteredItems);
} catch (err) {
reject(err);
}
});
});
}
/**
* Gets the node file system stats for the specified path
* @param {string} path
* @returns {Promise} Resolved path and stats
*/
private getStats(path: string): Promise<{ path: string, stats: fs.Stats }> {
return new Promise((resolve, reject) => {
fs.stat(path, (err, stats: fs.Stats) => {
if (err) {
reject(err);
}
resolve({
path,
stats,
});
});
});
}
}