mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-12 08:54:00 +02:00
d9d75d6c71
* Replace linked Note ids by relative paths in MD Exporter. * Added tests for the MD Exporter. * Changed fs.readdirSync use for earlier Node version (v8) In the previous commit the code used fs.readdirSync from Node v10 or later. But since Joplin still uses v8, I changed the use of fs.readdirSync to be in line with the earlier api. * Updated readDirSync use for Node v10, which allows gets folder names too. * Revert "Updated readDirSync use for Node v10, which allows gets folder names too." This reverts commit 8f255db120861dd7773d99e1b63f4864d39594cf. Because the Travis builds still use Node v8. This is fine as well, the readdirSync returns the filenames in the directory. * Added reservedNames param to findUniqueFilename
70 lines
2.0 KiB
JavaScript
70 lines
2.0 KiB
JavaScript
const { filename, fileExtension } = require('lib/path-utils');
|
|
const { time } = require('lib/time-utils.js');
|
|
|
|
class FsDriverBase {
|
|
async isDirectory(path) {
|
|
const stat = await this.stat(path);
|
|
return !stat ? false : stat.isDirectory();
|
|
}
|
|
|
|
async readDirStatsHandleRecursion_(basePath, stat, output, options) {
|
|
if (options.recursive && stat.isDirectory()) {
|
|
const subPath = `${basePath}/${stat.path}`;
|
|
const subStats = await this.readDirStats(subPath, options);
|
|
for (let j = 0; j < subStats.length; j++) {
|
|
const subStat = subStats[j];
|
|
subStat.path = `${stat.path}/${subStat.path}`;
|
|
output.push(subStat);
|
|
}
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
async findUniqueFilename(name, reservedNames = null) {
|
|
if (reservedNames === null) {
|
|
reservedNames = [];
|
|
}
|
|
let counter = 1;
|
|
|
|
let nameNoExt = filename(name, true);
|
|
let extension = fileExtension(name);
|
|
if (extension) extension = `.${extension}`;
|
|
let nameToTry = nameNoExt + extension;
|
|
while (true) {
|
|
// Check if the filename does not exist in the filesystem and is not reserved
|
|
const exists = await this.exists(nameToTry) || reservedNames.includes(nameToTry);
|
|
if (!exists) return nameToTry;
|
|
nameToTry = `${nameNoExt} (${counter})${extension}`;
|
|
counter++;
|
|
if (counter >= 1000) nameToTry = `${nameNoExt} (${new Date().getTime()})${extension}`;
|
|
if (counter >= 10000) throw new Error('Cannot find unique title');
|
|
}
|
|
}
|
|
|
|
async removeAllThatStartWith(dirPath, filenameStart) {
|
|
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) {
|
|
await this.remove(`${dirPath}/${stat.path}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
async waitTillExists(path, timeout = 10000) {
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = FsDriverBase;
|