UNPKG

ownfiles

Version:

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

102 lines 4.37 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.renameFile = exports.renameFolder = void 0; const mime_1 = __importDefault(require("mime")); const url_1 = __importDefault(require("url")); const rdf = __importStar(require("rdflib")); const solid_namespace_1 = __importDefault(require("solid-namespace")); exports.renameFolder = function (resource, newName) { return new Promise((resolve, reject) => { this.fetcher.load(resource).then(() => { const oldFolderName = resource.split('/'); if (resource.endsWith('/')) { oldFolderName.pop(); oldFolderName.pop(); } else { oldFolderName.pop(); } const newFolderLocation = oldFolderName.join('/'); oldFolderName.push(newName); const newFolderUrl = oldFolderName.join('/'); this.create(newFolderUrl + '/').then((res) => { var _a; const newFolderName = (_a = res.headers.get('location')) !== null && _a !== void 0 ? _a : newFolderUrl + '/'; console.log(`DEBUG -- Successfully copied ${resource} to ${newFolderName}`); 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(newFolderLocation, newFolderName)); }); Promise.all(copies) .then(() => { this.delete(resource) .then(() => { console.log(`DEBUG -- Successfully renamed ${resource} to ${newName}`); resolve(); }) .catch((err) => { reject(err); }); }) .catch((err) => { reject(err); }); }); }); }); }; exports.renameFile = function (resource, newName) { return this.read(resource).then((content) => { var _a; const stringContent = content; const blobContent = content; const options = { name: newName, contents: (_a = stringContent !== null && stringContent !== void 0 ? stringContent : blobContent) !== null && _a !== void 0 ? _a : '', contentType: mime_1.default.getType(newName) || mime_1.default.getType(resource) || 'text/turtle', }; let location = ''; if (resource.endsWith('/')) { const resourceFragments = resource.split('/'); resourceFragments.pop(); resourceFragments.pop(); location = resourceFragments.join('/'); } else { const resourceFragments = resource.split('/'); resourceFragments.pop(); location = resourceFragments.join('/'); } const newLocation = url_1.default.resolve(location, newName); return this.create(newLocation, options).then(() => { console.log(`DEBUG -- Successfully renamed ${resource} to ${newName}`); return this.delete(resource); }); }); }; //# sourceMappingURL=rename.js.map