image-tagger/module/utils/fs.mjs

49 lines
988 B
JavaScript

import { __ID__, filePath } from "../consts.mjs";
const { fetchJsonWithTimeout } = foundry.utils;
export async function lastModifiedAt(path) {
try {
const response = await fetch(filePath(path), { method: `HEAD` });
return response.headers.get(`Last-Modified`);
} catch {
return null;
};
};
export async function getFile(path) {
try {
return fetchJsonWithTimeout(filePath(path));
} catch {
return undefined;
};
};
/**
* @param {string} path
* @param {any} data
*/
export async function uploadJson(path, filename, data) {
// uploadPersistent adds "storage" into the path automatically
if (path.startsWith(`storage/`)) {
path = path.slice(8);
};
if (path.endsWith(`/`)) {
path = path.slice(0, -1);
};
const picker = foundry.applications.apps.FilePicker.implementation;
try {
const file = new File(
[JSON.stringify(data)],
filename,
{ type: `text/plain` },
);
await picker.uploadPersistent(
__ID__,
path,
file,
);
} catch {};
};