forked from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutilities.js
115 lines (96 loc) · 2.62 KB
/
utilities.js
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
const { requestUrl } = require("obsidian");
const fs = require("fs");
const moment = require("moment");
const axios = require("axios");
const path = require("path");
async function downloadImage(url, folderName, app) {
const vaultPath = app.vault.adapter.basePath;
const response = await axios.get(url, {
responseType: "arraybuffer",
});
const filename = path.join(vaultPath, folderName, path.basename(url));
fs.writeFileSync(filename, response.data);
return filename;
}
const createFolder = async (app) => {
const vaultPath = app.vault.adapter.basePath;
const folderName = moment().format("YYYY-MM-DD-HH-mm-ss");
return new Promise((resolve, reject) => {
fs.mkdir(path.join(vaultPath, folderName), (err) => {
if (err) {
reject(err);
}
resolve(folderName);
});
});
};
async function downloadFile(url, outputPath, app) {
const vaultPath = app.vault.adapter.basePath;
try {
const response = await requestUrl({
url: url,
method: "GET",
headers: {
"Content-Type": "application/octet-stream",
},
mode: "no-cors",
cache: "default",
});
console.log("Received response:", response);
if (!response) {
throw new Error(`Failed to download file.`);
} else {
console.log("response", response);
}
// Convert the response to an ArrayBuffer
const arrayBuffer = await response.arrayBuffer;
// Write the ArrayBuffer to the filesystem
fs.writeFileSync(
path.join(vaultPath, outputPath),
new Uint8Array(arrayBuffer)
);
} catch (error) {
console.error("Error in downloadFile:", error);
throw error;
}
}
const getImageExtension = (url) => {
const extensionMatch = url.match(/\.(jpeg|jpg|gif|png)/);
return extensionMatch ? extensionMatch[1] : "png";
};
const sanitizeTitle = (title) => {
// Remove special characters and limit length
return title
.replace(/[^a-zA-Z0-9- ]/g, "") // Keep spaces and remove underscores from the regular expression
.substring(0, 200);
};
const generateUniqueTitle = (title, folderPath) => {
let uniqueTitle = title;
let counter = 1;
while (fs.existsSync(`${folderPath}/${uniqueTitle}.md`)) {
uniqueTitle = `${title} (${counter})`;
counter += 1;
}
return uniqueTitle;
};
const writeFilePromise = (fileName, content) => {
return new Promise((resolve, reject) => {
fs.writeFile(fileName, content, function (err) {
if (err) {
console.error(`Error writing file ${fileName}: ${err}`);
reject(err);
} else {
resolve(`${fileName} was saved!`);
}
});
});
};
module.exports = {
downloadImage,
createFolder,
downloadFile,
getImageExtension,
sanitizeTitle,
writeFilePromise,
generateUniqueTitle
};