mirror of
https://github.com/HeyPuter/puter.git
synced 2026-09-22 21:26:04 +00:00
create getReadUrl
This commit is contained in:
@@ -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;
|
||||
Reference in New Issue
Block a user