UNPKG

ownfiles

Version:

A library to manage files in a Solid User's Pod

151 lines 5.76 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getFolderResult = exports.read = exports.ReadException = void 0; const url_1 = __importDefault(require("url")); const mime_1 = __importDefault(require("mime")); const rdf = __importStar(require("rdflib")); const solid_namespace_1 = __importDefault(require("solid-namespace")); function ReadException(response) { this.response = response; } exports.ReadException = ReadException; exports.read = async function (resource, _a) { var _b, _c, _d; var { auth, verbose, headOnly, headers } = _a === void 0 ? { auth: false, verbose: false, headOnly: false, headers: { Accept: (_b = mime_1.default.getType(resource)) !== null && _b !== void 0 ? _b : 'text/turtle', }, } : _a; const store = this.graph; const fetcher = this.fetcher; store.removeDocument(rdf.sym(resource)); let fetch; if (auth) { fetch = auth.fetch; } else { fetch = fetcher._fetch; } const headResponse = await fetch(resource, { method: 'HEAD', }); if (headResponse.status !== 200) { throw new ReadException(headResponse); } const isFolder = headResponse && (((_c = headResponse.headers .get('Link')) === null || _c === void 0 ? void 0 : _c.includes('http://www.w3.org/ns/ldp#Container')) || (((_d = headResponse.headers .get('Link')) === null || _d === void 0 ? void 0 : _d.includes('http://www.w3.org/ns/ldp#Resource')) && resource.endsWith('/'))); if (!isFolder && headOnly) { return Promise.resolve(verbose ? { name: resource, type: mime_1.default.getType(resource), } : resource); } resource = isFolder ? resource.endsWith('/') ? resource : resource + '/' : resource; const response = await fetch(resource, { headers: headers, }); const contentType = response.headers.get('Content-Type'); if (contentType.includes('text') || (contentType.includes('application') && contentType !== 'application/octet-stream')) { const text = await response.text(); if (response.headers.get('Content-Type') === 'text/turtle' && isFolder) { rdf.parse(text, store, resource); return exports.getFolderResult(resource, store, verbose); } else { if (verbose) { return { body: text, contentType: contentType }; } else { return text; } } } else { const body = response.blob ? await response.blob() : await response.buffer(); if (verbose) { return { body: body, contentType: contentType }; } else { return body; } } }; exports.getFolderResult = (resource, store, verbose = false) => { const containments = { folders: [], files: [] }; store .each(rdf.sym(resource), solid_namespace_1.default(rdf).ldp('contains'), null) .map((result) => { return result.value; }) .forEach((result) => { var _a, _b, _c; const type = store.statementsMatching(rdf.sym(result), solid_namespace_1.default(rdf).rdf('type'))[0].object; const isLinked = ((type) => type.value === 'http://www.w3.org/ns/iana/media-types/text/turtle#Resource')(type); let resultFragments = (_a = url_1.default.parse(result).pathname) === null || _a === void 0 ? void 0 : _a.split('/'); if (resultFragments && resultFragments[1] === '') { resultFragments.shift(); const path = (_b = url_1.default.parse(result).pathname) !== null && _b !== void 0 ? _b : result.substr(result.replace('https://', '').indexOf('/'), result.length); result = result.replace(path, resultFragments.join('/')); resultFragments = path.split('/'); } if (resultFragments && resultFragments[resultFragments.length - 1] === '') { containments.folders.push(result.endsWith('/') ? result : result + '/'); } else { if (verbose) { containments.files.push({ name: result, type: !isLinked ? (_c = mime_1.default.getType(result)) !== null && _c !== void 0 ? _c : 'text/plain' : 'text/turtle', }); } else { containments.files.push(result); } } }); return containments; }; //# sourceMappingURL=read.js.map