-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.mjs
71 lines (60 loc) · 1.73 KB
/
util.mjs
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
import urlRegexSafe from "url-regex-safe";
import * as fs from "node:fs";
import { exit } from "process";
export const isContentTypeImageType = function (contentType) {
return contentType.includes("image");
};
// Check url type
// Code is modified based on https://github.com/haorendashu/nostrmo/blob/main/lib/component/content/content_decoder.dart#L505
export const getUrlType = function (path) {
var strs = path.split("?");
var index = strs[0].lastIndexOf(".");
if (index == -1) {
return "unknown";
}
path = strs[0];
var n = path.substring(index);
n = n.toLowerCase();
if (n == ".png" ||
n == ".jpg" ||
n == ".jpeg" ||
n == ".gif" ||
n == ".webp") {
return "image";
} else if (n == ".mp4" || n == ".mov" || n == ".wmv" || n == ".webm" || n == ".avi") {
return "video";
} else {
return "link";
}
}
export const extractUrl = function (text) {
const matches = text.match(
urlRegexSafe({ strict: true, localhost: false, returnString: false })
);
return matches;
};
export const cleanUrlWithoutParam = function (url) {
const newUrl = new URL(url);
newUrl.search = "";
return newUrl.toString();
};
export const handleFatalError = function (err) {
if (typeof err === "undefined") return;
if (err === null) return;
console.error(err);
// force exit
exit(1);
};
export async function deleteFile(filePath) {
return new Promise((resolve, reject) => {
fs.unlink(filePath, (err) => {
if (err) reject(err);
resolve(true);
});
});
}
export const nMinutesAgo = (n) => Math.floor((Date.now() - n * 60 * 1000) / 1000);
export const truncateString = (text, n) => {
let chars = Array.from(String(text));
return (chars.length > n) ? chars.slice(0, n - 1).join('') : text;
}