2022-04-07 19:18:22 -08:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
|
2023-11-10 15:24:31 -08:00
|
|
|
export const fileUploadSizeLimit = 50 * 1024 * 1024;
|
|
|
|
|
2022-04-07 19:18:22 -08:00
|
|
|
export const existsAsync = (path: string): Promise<boolean> => new Promise(resolve => fs.stat(path, err => resolve(!err)));
|
|
|
|
|
|
|
|
export async function mkdirIfNeeded(filePath: string) {
|
|
|
|
// This will harmlessly throw on windows if the dirname is the root directory.
|
|
|
|
await fs.promises.mkdir(path.dirname(filePath), { recursive: true }).catch(() => {});
|
|
|
|
}
|
|
|
|
|
2023-10-26 19:41:00 +02:00
|
|
|
export async function removeFolders(dirs: string[]): Promise<Error[]> {
|
|
|
|
return await Promise.all(dirs.map((dir: string) =>
|
2023-11-20 18:58:10 +09:00
|
|
|
fs.promises.rm(dir, { recursive: true, force: true, maxRetries: 10 }).catch(e => e)
|
|
|
|
));
|
2022-04-07 19:18:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function canAccessFile(file: string) {
|
|
|
|
if (!file)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.accessSync(file);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-11-13 12:46:35 -08:00
|
|
|
|
|
|
|
export async function copyFileAndMakeWritable(from: string, to: string) {
|
|
|
|
await fs.promises.copyFile(from, to);
|
|
|
|
await fs.promises.chmod(to, 0o664);
|
|
|
|
}
|
2023-08-03 13:23:30 -07:00
|
|
|
|
|
|
|
export function sanitizeForFilePath(s: string) {
|
|
|
|
return s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
|
|
|
|
}
|
2024-01-16 13:32:47 -08:00
|
|
|
|
|
|
|
export function toPosixPath(aPath: string): string {
|
|
|
|
return aPath.split(path.sep).join(path.posix.sep);
|
|
|
|
}
|