UNPKG

@fairdatasociety/fairdrive-opfs

Version:

Fairdrive OPFS - integrate data sources from Web 2.0 or Web 3.0

243 lines (242 loc) 7.26 kB
import fetch from 'node-fetch'; import { FdpConnectProvider } from '../core/provider'; /** * FairosProviderDriver is the driver for the FairOS provider. */ export class FairosProviderDriver { constructor(options) { this.host = options.host; } /** * Verify if a file exists * @param path - path to the file * @param mount - mount to check * @returns Returns true if the file exists, else false */ async exists(path, mount) { const res = await fetch(`${this.host}v1/file/stat?filePath=${path}&podName=${mount.name}`, { method: 'GET', headers: { 'Content-Type': 'application/json', }, credentials: 'include', }); if (res.status === 200) { const data = await res.json(); return data.filePath === path && data.podName === mount.name; } else { return false; } } async createDir(name, mount) { const data = { dirPath: name, podName: mount.name, }; const res = await fetch(`${this.host}v1/dir/mkdir`, { method: 'POST', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json', }, credentials: 'include', }); return (await res.json()).dirPath === name; } async delete(filePath, mount) { const data = { filePath, podName: mount.name, }; const res = await fetch(`${this.host}v1/file/delete`, { method: 'DELETE', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json', }, credentials: 'include', }); return (await res.json()).filePath === filePath; } async read(mount) { const res = await fetch(`${this.host}v1/dir/ls?dirPath=${mount.path}&podName=${mount.name}`, { method: 'GET', headers: { 'Content-Type': 'application/json', }, credentials: 'include', }); if (res.status === 200) { const data = await res.json(); return { dirs: (data.dirs || []).map((dir) => dir.name), files: (data.files || []).map((file) => file.name), mount, }; } else { return { dirs: [], files: [], mount, }; } } /** * Download a file * @param id - id of the file * @param mount - mount to download the file from * @param options - options * @returns */ async download(id, mount) { const data = { filePath: id, podName: mount.name, }; const res = await fetch(this.host + 'v1/file/download' + '?' + new URLSearchParams(data), { method: 'POST', mode: 'cors', body: JSON.stringify(data), credentials: 'include', headers: { 'Content-Type': 'application/json', }, }); const a = await res.arrayBuffer(); return new Uint8Array(a); } /** * Upload a file * @param file - file to upload * @param mount - mount to upload to * @param options - options */ async upload(file, mount, options = { overwrite: false, path: '/' }) { const formData = new FormData(); formData.append('files', file); formData.set('podName', mount.name); formData.append('fileName', file.name); //"index.json"); formData.set('dirPath', mount.path); // "/"); formData.set('blockSize', '1Mb'); if (options.overwrite === true) formData.set('overwrite', 'true'); const res = await fetch(this.host + 'v1/file/upload', { method: 'POST', body: formData, credentials: 'include', }); return res.json(); } } /** * FairosProvider is the provider for the FairOS provider. */ export class FairosProvider extends FdpConnectProvider { constructor(host = 'https://fairos.dev.fairdatasociety.org/') { super({ name: 'FairosProvider', }); this.host = host; } /** * Initialize the provider * @param options options */ initialize(options) { super.initialize(options); this.onMount.subscribe(async (mount) => { if (mount.name !== this.getCurrentMount().name) { this.podClose(mount); } this.podOpen(mount); }); this.filesystemDriver = new FairosProviderDriver(options); } /** * Login a user * @param user - username * @param pass - password * @returns Returns a promise with the response */ async userLogin(user, pass) { const data = { userName: user, password: pass, }; return await fetch(this.host + 'v2/user/login', { method: 'POST', mode: 'cors', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), credentials: 'include', }); } /** * Verify if a user is logged in * @param username - username * @returns Returns a promise with the response */ async userLoggedIn(username) { const data = { userName: username, }; return await fetch(this.host + 'v1/user/isloggedin' + '?' + new URLSearchParams(data), { method: 'GET', headers: { 'Content-Type': 'application/json', }, credentials: 'include', }); } async listMounts() { const res = await fetch(`${this.host}v1/pod/ls?podName=`, { method: 'GET', headers: { 'Content-Type': 'application/json', }, credentials: 'include', }); if (res.status === 200) { const data = await res.json(); return data.pods.map((name) => { return { name, path: '/', }; }); } else { return []; } } async podOpen(mount) { const res = await fetch(`${this.host}v1/pod/open`, { method: 'POST', body: JSON.stringify({ podName: mount.name }), headers: { 'Content-Type': 'application/json', }, credentials: 'include', }); if (res.status !== 200) { throw new Error(res.message); } } async podClose(mount) { const res = await fetch(`${this.host}v1/pod/close`, { method: 'POST', body: JSON.stringify({ podName: mount.name }), headers: { 'Content-Type': 'application/json', }, credentials: 'include', }); if (res.status !== 200) { throw new Error(res.message); } } }