UNPKG

ownfiles

Version:

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

95 lines 4.29 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.copyFolder = exports.copyFile = exports.copy = void 0; const url_1 = __importDefault(require("url")); const rdf = __importStar(require("rdflib")); const solid_namespace_1 = __importDefault(require("solid-namespace")); exports.copy = function (resource, location) { return new Promise((resolve, reject) => this.fetcher.load(location).then(async () => { if (this.graph.each(rdf.sym(location), solid_namespace_1.default(rdf).rdf('type'), solid_namespace_1.default(rdf).ldp('Container'), rdf.sym(location).doc()).length < 0) { throw new Error('The specified Location is not a folder'); } else { try { if (resource.endsWith('/')) { await this.copyFolder(resource, location); } else { await this.copyFile(resource, location); } resolve(); } catch (err) { reject(err); } } })); }; exports.copyFile = function copyFile(resource, location) { return this.read(resource, { verbose: true }).then((file) => { file = file; const fileName = resource.substr(resource.lastIndexOf('/') + 1); const options = { name: fileName, contentType: file.contentType, contents: file.body, }; location = location.endsWith('/') ? location : location + '/'; const newLocation = url_1.default.resolve(location, fileName); return this.create(newLocation, options).then((res) => { console.log(`DEBUG -- Successfully copied ${resource} to ${location}`); return res; }); }); }; exports.copyFolder = function copyFolder(resource, location) { location = location.endsWith('/') ? location : location + '/'; return new Promise((resolve, reject) => { this.fetcher.load(resource).then(() => { const oldFolderNameFragments = resource.split('/'); const oldFolderName = oldFolderNameFragments[oldFolderNameFragments.length - 2]; const newFolderUrl = url_1.default.resolve(location, oldFolderName); this.create(newFolderUrl + '/').then((res) => { var _a; const newFolderName = (_a = res.headers.get('location')) !== null && _a !== void 0 ? _a : newFolderUrl.substr(newFolderUrl.lastIndexOf('/'), newFolderUrl.length); console.log(`DEBUG -- Successfully copied ${resource} to ${location}`); const copies = this.graph .each(rdf.sym(resource), solid_namespace_1.default(rdf).ldp('contains')) .map((containment) => { return this.copy(containment.value, url_1.default.resolve(location, newFolderName)); }); Promise.all(copies) .then(() => { console.log(`DEBUG -- Successfully copied ${resource}'s contents to ${location}`); resolve(); }) .catch((err) => { reject(err); }); }); }); }); }; //# sourceMappingURL=copy.js.map