init
This commit is contained in:
100
ext/lib/paths.js
Normal file
100
ext/lib/paths.js
Normal file
@@ -0,0 +1,100 @@
|
||||
// Folder/filename scheme builder, shared by background + options preview.
|
||||
// Folder and file naming schemes.
|
||||
|
||||
export const SINGLE_SCHEMES = {
|
||||
one: { label: "One folder", desc: "Saves go straight into Downloads.", folders: [] },
|
||||
creator: { label: "By creator", desc: "A folder for each creator.", folders: ["{creator}"] },
|
||||
"creator-type": {
|
||||
label: "By creator and type",
|
||||
rec: true,
|
||||
desc: "A folder per creator, with Videos, Photos and Audio inside.",
|
||||
folders: ["{creator}", "{typeFolder}"],
|
||||
},
|
||||
"site-creator-type": {
|
||||
label: "By site, creator and type",
|
||||
desc: "Like By creator and type, with a folder for each site on top.",
|
||||
folders: ["OnlyFans", "{creator}", "{typeFolder}"],
|
||||
},
|
||||
};
|
||||
|
||||
export const BULK_SCHEMES = {
|
||||
"creator-section-type": {
|
||||
label: "By creator, section and type",
|
||||
rec: true,
|
||||
desc: "A folder per creator, then Timeline, Messages, Paid… with Videos and Photos inside.",
|
||||
folders: ["{creator}", "{section}", "{typeFolder}"],
|
||||
},
|
||||
same: { label: "Same as single downloads", desc: "Bulk downloads fill the folders your single downloads use.", same: true },
|
||||
"creator-date-section": {
|
||||
label: "By creator, date and section (classic)",
|
||||
desc: "A folder for the day each run starts, split by section.",
|
||||
folders: ["{creator}", "{date}", "{section}"],
|
||||
},
|
||||
one: { label: "One folder", desc: "Saves go straight into Downloads.", folders: [] },
|
||||
};
|
||||
|
||||
const TYPE_FOLDERS = { video: "Videos", photo: "Photos", gif: "Photos", audio: "Audio" };
|
||||
|
||||
/** Build the full download path (relative to Downloads).
|
||||
* opts: { scheme, bulk, creator, type, section, date(ts s|Date|str), timestamp, mediaId, postId, quality, ext, template } */
|
||||
export function buildPath(opts) {
|
||||
const scheme = opts.scheme ?? "creator-type";
|
||||
const def = opts.bulk ? BULK_SCHEMES[scheme] ?? BULK_SCHEMES.same : SINGLE_SCHEMES[scheme] ?? SINGLE_SCHEMES["creator-type"];
|
||||
const folders = def.same ? (SINGLE_SCHEMES[opts.singleScheme ?? "creator-type"] ?? SINGLE_SCHEMES["creator-type"]).folders : def.folders;
|
||||
|
||||
const type = opts.type ?? "video";
|
||||
const typeFolder = TYPE_FOLDERS[type] ?? "Other";
|
||||
const date = formatDate(opts.date);
|
||||
const creator = sanitize(opts.creator ?? "unknown");
|
||||
|
||||
const parts = folders.map((f) =>
|
||||
sanitize(
|
||||
f
|
||||
.replaceAll("{creator}", creator)
|
||||
.replaceAll("{typeFolder}", typeFolder)
|
||||
.replaceAll("{section}", sanitize(opts.section ?? "Timeline"))
|
||||
.replaceAll("{date}", date)
|
||||
)
|
||||
);
|
||||
|
||||
const name = buildName(opts);
|
||||
return [...parts, name].filter(Boolean).join("/");
|
||||
}
|
||||
|
||||
export function buildName(opts) {
|
||||
const type = opts.type ?? "video";
|
||||
const template = opts.template ?? "{creator}_{type}_{date}_{timestamp}_{mediaId}_{quality}";
|
||||
const out = template
|
||||
.replaceAll("{creator}", sanitize(opts.creator ?? "unknown"))
|
||||
.replaceAll("{type}", type)
|
||||
.replaceAll("{date}", formatDate(opts.date))
|
||||
.replaceAll("{timestamp}", String(opts.timestamp ?? Math.floor(Date.now() / 1000)))
|
||||
.replaceAll("{mediaId}", String(opts.mediaId ?? ""))
|
||||
.replaceAll("{postId}", String(opts.postId ?? ""))
|
||||
.replaceAll("{quality}", opts.quality ?? "")
|
||||
.replaceAll("{ext}", opts.ext ?? "mp4")
|
||||
.replace(/_+(\.)/g, "$1")
|
||||
.replace(/_{2,}/g, "_");
|
||||
const out2 = sanitize(out);
|
||||
// always end with a sensible extension
|
||||
const ext = (opts.ext ?? "mp4").replace(/^\./, "");
|
||||
return out2.toLowerCase().endsWith("." + ext.toLowerCase()) ? out2 : `${out2.replace(/_+$/, "")}.${ext}`;
|
||||
}
|
||||
|
||||
function formatDate(d) {
|
||||
if (!d) return "";
|
||||
let date;
|
||||
if (d instanceof Date) date = d;
|
||||
else if (typeof d === "number") date = new Date(d * (d > 1e12 ? 1 : 1000));
|
||||
else {
|
||||
const parsed = Date.parse(d);
|
||||
if (Number.isNaN(parsed)) return sanitize(String(d));
|
||||
date = new Date(parsed);
|
||||
}
|
||||
const p = (n) => String(n).padStart(2, "0");
|
||||
return `${date.getFullYear()}-${p(date.getMonth() + 1)}-${p(date.getDate())}`;
|
||||
}
|
||||
|
||||
function sanitize(s) {
|
||||
return String(s ?? "").replace(/[^\w.-]+/g, "_").replace(/^\.+/, "_");
|
||||
}
|
||||
Reference in New Issue
Block a user