mirror of
https://github.com/HeyPuter/puter.git
synced 2026-04-28 03:01:29 -05:00
move: PuterAPIFilesystem
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import * as utils from '../../lib/utils.js';
|
||||
import putility from "@heyputer/putility";
|
||||
import { TeePromise } from "@heyputer/putility/src/libs/promise";
|
||||
import getAbsolutePathForApp from './utils/getAbsolutePathForApp.js';
|
||||
import { TFilesystem } from './definitions.js';
|
||||
|
||||
export class PuterAPIFilesystem extends putility.AdvancedBase {
|
||||
constructor ({ api_info }) {
|
||||
super();
|
||||
this.api_info = api_info;
|
||||
}
|
||||
|
||||
static IMPLEMENTS = {
|
||||
[TFilesystem]: {
|
||||
stat: async function (options) {
|
||||
this.ensure_auth_();
|
||||
const tp = new TeePromise();
|
||||
|
||||
const xhr = new utils.initXhr('/stat', this.api_info.APIOrigin, this.api_info.authToken);
|
||||
utils.setupXhrEventHandlers(xhr, undefined, undefined,
|
||||
tp.resolve.bind(tp),
|
||||
tp.reject.bind(tp),
|
||||
);
|
||||
|
||||
let dataToSend = {};
|
||||
if (options.uid !== undefined) {
|
||||
dataToSend.uid = options.uid;
|
||||
} else if (options.path !== undefined) {
|
||||
// If dirPath is not provided or it's not starting with a slash, it means it's a relative path
|
||||
// in that case, we need to prepend the app's root directory to it
|
||||
dataToSend.path = getAbsolutePathForApp(options.path);
|
||||
}
|
||||
|
||||
dataToSend.return_subdomains = options.returnSubdomains;
|
||||
dataToSend.return_permissions = options.returnPermissions;
|
||||
dataToSend.return_versions = options.returnVersions;
|
||||
dataToSend.return_size = options.returnSize;
|
||||
|
||||
xhr.send(JSON.stringify(dataToSend));
|
||||
|
||||
return await tp;
|
||||
},
|
||||
readdir: async function (options) {
|
||||
this.ensure_auth_();
|
||||
const tp = new TeePromise();
|
||||
|
||||
const xhr = new utils.initXhr('/readdir', this.api_info.APIOrigin, this.api_info.authToken);
|
||||
utils.setupXhrEventHandlers(xhr, undefined, undefined,
|
||||
tp.resolve.bind(tp),
|
||||
tp.reject.bind(tp),
|
||||
);
|
||||
|
||||
xhr.send(JSON.stringify({path: getAbsolutePathForApp(options.path)}));
|
||||
|
||||
return await tp;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
ensure_auth_ () {
|
||||
// TODO: remove reference to global 'puter'; get 'env' via context
|
||||
if ( ! this.api_info.authToken && puter.env === 'web' ) {
|
||||
try {
|
||||
this.ui.authenticateWithPuter();
|
||||
} catch (e) {
|
||||
throw new Error('Authentication failed.');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,4 @@
|
||||
import * as utils from '../../lib/utils.js';
|
||||
import putility from "@heyputer/putility";
|
||||
import { TeePromise } from "@heyputer/putility/src/libs/promise";
|
||||
import getAbsolutePathForApp from './utils/getAbsolutePathForApp.js';
|
||||
|
||||
export const TFilesystem = 'TFilesystem';
|
||||
|
||||
@@ -20,71 +17,6 @@ export const IFilesystem = {
|
||||
|
||||
};
|
||||
|
||||
export class PuterAPIFilesystem extends putility.AdvancedBase {
|
||||
constructor ({ api_info }) {
|
||||
super();
|
||||
this.api_info = api_info;
|
||||
}
|
||||
|
||||
static IMPLEMENTS = {
|
||||
[TFilesystem]: {
|
||||
stat: async function (options) {
|
||||
this.ensure_auth_();
|
||||
const tp = new TeePromise();
|
||||
|
||||
const xhr = new utils.initXhr('/stat', this.api_info.APIOrigin, this.api_info.authToken);
|
||||
utils.setupXhrEventHandlers(xhr, undefined, undefined,
|
||||
tp.resolve.bind(tp),
|
||||
tp.reject.bind(tp),
|
||||
);
|
||||
|
||||
let dataToSend = {};
|
||||
if (options.uid !== undefined) {
|
||||
dataToSend.uid = options.uid;
|
||||
} else if (options.path !== undefined) {
|
||||
// If dirPath is not provided or it's not starting with a slash, it means it's a relative path
|
||||
// in that case, we need to prepend the app's root directory to it
|
||||
dataToSend.path = getAbsolutePathForApp(options.path);
|
||||
}
|
||||
|
||||
dataToSend.return_subdomains = options.returnSubdomains;
|
||||
dataToSend.return_permissions = options.returnPermissions;
|
||||
dataToSend.return_versions = options.returnVersions;
|
||||
dataToSend.return_size = options.returnSize;
|
||||
|
||||
xhr.send(JSON.stringify(dataToSend));
|
||||
|
||||
return await tp;
|
||||
},
|
||||
readdir: async function (options) {
|
||||
this.ensure_auth_();
|
||||
const tp = new TeePromise();
|
||||
|
||||
const xhr = new utils.initXhr('/readdir', this.api_info.APIOrigin, this.api_info.authToken);
|
||||
utils.setupXhrEventHandlers(xhr, undefined, undefined,
|
||||
tp.resolve.bind(tp),
|
||||
tp.reject.bind(tp),
|
||||
);
|
||||
|
||||
xhr.send(JSON.stringify({path: getAbsolutePathForApp(options.path)}));
|
||||
|
||||
return await tp;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
ensure_auth_ () {
|
||||
// TODO: remove reference to global 'puter'; get 'env' via context
|
||||
if ( ! this.api_info.authToken && puter.env === 'web' ) {
|
||||
try {
|
||||
this.ui.authenticateWithPuter();
|
||||
} catch (e) {
|
||||
throw new Error('Authentication failed.');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class ProxyFilesystem extends putility.AdvancedBase {
|
||||
static PROPERTIES = {
|
||||
delegate: () => {},
|
||||
|
||||
@@ -13,9 +13,10 @@ import sign from "./operations/sign.js";
|
||||
// Why is this called deleteFSEntry instead of just delete? because delete is
|
||||
// a reserved keyword in javascript
|
||||
import deleteFSEntry from "./operations/deleteFSEntry.js";
|
||||
import { ProxyFilesystem, PuterAPIFilesystem, TFilesystem } from './definitions.js';
|
||||
import { ProxyFilesystem, TFilesystem } from './definitions.js';
|
||||
import { AdvancedBase } from '../../../../putility/index.js';
|
||||
import { CachedFilesystem } from './CacheFS.js';
|
||||
import { PuterAPIFilesystem } from './APIFS.js';
|
||||
|
||||
export class PuterJSFileSystemModule extends AdvancedBase {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user