create getReadUrl

This commit is contained in:
ProgrammerIn-wonderland
2025-08-26 18:17:03 -04:00
parent d2ea480dc6
commit 542cda38b7
2 changed files with 45 additions and 1 deletions
+3 -1
View File
@@ -16,6 +16,7 @@ import symlink from './operations/symlink.js';
import deleteFSEntry from "./operations/deleteFSEntry.js";
import { AdvancedBase } from '../../../../putility/index.js';
import FSItem from '../FSItem.js';
import getReadUrl from './operations/getReadUrl.js';
export class PuterJSFileSystemModule extends AdvancedBase {
@@ -32,7 +33,8 @@ export class PuterJSFileSystemModule extends AdvancedBase {
write = write;
sign = sign;
symlink = symlink;
getReadUrl = getReadUrl;
FSItem = FSItem
static NARI_METHODS = {
@@ -0,0 +1,42 @@
import * as utils from '../../../lib/utils.js';
import stat from "./stat.js";
const getReadUrl = async function (path, expiresIn = "24h") {
return new Promise(async (resolve, reject) => {
// If auth token is not provided and we are in the web environment,
// try to authenticate with Puter
if (!puter.authToken && puter.env === 'web') {
try {
await puter.ui.authenticateWithPuter();
} catch (e) {
// if authentication fails, throw an error
reject('Authentication failed.');
}
}
try {
const { uid, is_dir } = (await stat.call(this, path))
if (is_dir) {
reject("Cannot create readUrl for directory");
return;
}
const xhr = utils.initXhr('/auth/create-access-token', this.APIOrigin, this.authToken);
utils.setupXhrEventHandlers(xhr, () => {}, () => {}, ({token})=> {
resolve(`${this.APIOrigin}/token-read?uid=${encodeURIComponent(uid)}&token=${encodeURIComponent(token)}`)
}, reject);
xhr.send(JSON.stringify({
expiresIn,
permissions: [
`fs:${uid}:read`,
]
}))
} catch (e) {
reject(e)
}
});
}
export default getReadUrl;