2020-12-09 14:50:51 +02:00
|
|
|
import time from './time';
|
|
|
|
import Setting from './models/Setting';
|
|
|
|
import { filename, fileExtension } from './path-utils';
|
2020-03-10 01:24:57 +02:00
|
|
|
const md5 = require('md5');
|
2018-11-20 02:42:21 +02:00
|
|
|
|
2020-12-09 14:50:51 +02:00
|
|
|
export interface Stat {
|
|
|
|
birthtime: number;
|
|
|
|
mtime: number;
|
|
|
|
isDirectory(): boolean;
|
|
|
|
path: string;
|
|
|
|
size: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ReadDirStatsOptions {
|
|
|
|
recursive: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class FsDriverBase {
|
|
|
|
|
|
|
|
public async stat(_path: string): Promise<Stat> {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
|
|
|
|
public async readDirStats(_path: string, _options: ReadDirStatsOptions = null): Promise<Stat[]> {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
|
|
|
|
public async exists(_path: string): Promise<boolean> {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
|
|
|
|
public async remove(_path: string): Promise<void> {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
|
|
|
|
public async isDirectory(path: string) {
|
2018-02-25 23:08:32 +02:00
|
|
|
const stat = await this.stat(path);
|
|
|
|
return !stat ? false : stat.isDirectory();
|
|
|
|
}
|
|
|
|
|
2020-12-09 14:50:51 +02:00
|
|
|
public async writeFile(_path: string, _content: string, _encoding: string = 'base64'): Promise<void> {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async readDirStatsHandleRecursion_(basePath: string, stat: Stat, output: Stat[], options: ReadDirStatsOptions): Promise<Stat[]> {
|
2018-02-25 23:08:32 +02:00
|
|
|
if (options.recursive && stat.isDirectory()) {
|
2019-09-19 23:51:18 +02:00
|
|
|
const subPath = `${basePath}/${stat.path}`;
|
2018-02-25 23:08:32 +02:00
|
|
|
const subStats = await this.readDirStats(subPath, options);
|
|
|
|
for (let j = 0; j < subStats.length; j++) {
|
|
|
|
const subStat = subStats[j];
|
2019-09-19 23:51:18 +02:00
|
|
|
subStat.path = `${stat.path}/${subStat.path}`;
|
2018-02-25 23:08:32 +02:00
|
|
|
output.push(subStat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2020-12-09 14:50:51 +02:00
|
|
|
public async findUniqueFilename(name: string, reservedNames: string[] = null): Promise<string> {
|
2020-01-18 15:16:14 +02:00
|
|
|
if (reservedNames === null) {
|
|
|
|
reservedNames = [];
|
|
|
|
}
|
2018-11-20 02:42:21 +02:00
|
|
|
let counter = 1;
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const nameNoExt = filename(name, true);
|
2018-11-20 02:42:21 +02:00
|
|
|
let extension = fileExtension(name);
|
2019-09-19 23:51:18 +02:00
|
|
|
if (extension) extension = `.${extension}`;
|
2018-11-20 02:42:21 +02:00
|
|
|
let nameToTry = nameNoExt + extension;
|
|
|
|
while (true) {
|
2020-01-18 15:16:14 +02:00
|
|
|
// Check if the filename does not exist in the filesystem and is not reserved
|
|
|
|
const exists = await this.exists(nameToTry) || reservedNames.includes(nameToTry);
|
2018-11-20 02:42:21 +02:00
|
|
|
if (!exists) return nameToTry;
|
2019-09-19 23:51:18 +02:00
|
|
|
nameToTry = `${nameNoExt} (${counter})${extension}`;
|
2018-11-20 02:42:21 +02:00
|
|
|
counter++;
|
2020-02-11 16:14:04 +02:00
|
|
|
if (counter >= 1000) {
|
|
|
|
nameToTry = `${nameNoExt} (${new Date().getTime()})${extension}`;
|
|
|
|
await time.msleep(10);
|
|
|
|
}
|
|
|
|
if (counter >= 1100) throw new Error('Cannot find unique filename');
|
2018-11-20 02:42:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 14:50:51 +02:00
|
|
|
public async removeAllThatStartWith(dirPath: string, filenameStart: string) {
|
2019-05-11 12:46:13 +02:00
|
|
|
if (!filenameStart || !dirPath) throw new Error('dirPath and filenameStart cannot be empty');
|
|
|
|
|
|
|
|
const stats = await this.readDirStats(dirPath);
|
|
|
|
|
|
|
|
for (const stat of stats) {
|
|
|
|
if (stat.path.indexOf(filenameStart) === 0) {
|
2019-09-19 23:51:18 +02:00
|
|
|
await this.remove(`${dirPath}/${stat.path}`);
|
2019-05-11 12:46:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 14:50:51 +02:00
|
|
|
public async waitTillExists(path: string, timeout: number = 10000) {
|
2019-05-12 02:15:52 +02:00
|
|
|
const startTime = Date.now();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const e = await this.exists(path);
|
|
|
|
if (e) return true;
|
|
|
|
if (Date.now() - startTime > timeout) return false;
|
|
|
|
await time.msleep(100);
|
|
|
|
}
|
|
|
|
}
|
2020-03-10 01:24:57 +02:00
|
|
|
|
|
|
|
// TODO: move out of here and make it part of joplin-renderer
|
|
|
|
// or assign to option using .bind(fsDriver())
|
2020-12-09 14:50:51 +02:00
|
|
|
public async cacheCssToFile(cssStrings: string[]) {
|
2020-03-23 02:47:25 +02:00
|
|
|
const cssString = Array.isArray(cssStrings) ? cssStrings.join('\n') : cssStrings;
|
2020-03-10 01:24:57 +02:00
|
|
|
const cssFilePath = `${Setting.value('tempDir')}/${md5(escape(cssString))}.css`;
|
|
|
|
if (!(await this.exists(cssFilePath))) {
|
|
|
|
await this.writeFile(cssFilePath, cssString, 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
path: cssFilePath,
|
|
|
|
mime: 'text/css',
|
|
|
|
};
|
|
|
|
}
|
2018-02-25 23:08:32 +02:00
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
public async tarExtract(_options: any) {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
|
|
|
|
public async tarCreate(_options: any, _filePaths: string[]) {
|
|
|
|
throw new Error('Not implemented');
|
|
|
|
}
|
|
|
|
|
2020-12-09 14:50:51 +02:00
|
|
|
}
|