ownfiles
Version:
A library to manage files in a Solid User's Pod
135 lines • 5.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createIfNotExist = exports.createFile = exports.createFolder = exports.create = void 0;
const url_1 = __importDefault(require("url"));
const mime_1 = __importDefault(require("mime"));
const rdflib_1 = require("rdflib");
const solid_namespace_1 = __importDefault(require("solid-namespace"));
const typeNamespaceUrl = 'http://www.w3.org/ns/iana/media-types/';
const types = rdflib_1.Namespace(typeNamespaceUrl);
exports.create = function (resourceAddress, options = {}) {
if (!resourceAddress) {
throw new Error('Please specify the location for the new resource.');
}
else if (url_1.default.parse(resourceAddress).protocol !== 'https:') {
throw new Error('Please specify a valid location for the new resource.');
}
const paths = resourceAddress.split('/');
if (!options.name) {
if (paths[paths.length - 1] !== '') {
options.name = paths[paths.length - 1];
paths.pop();
return this.createFile(paths.join('/') + '/', options);
}
else {
options.name = paths[paths.length - 2];
paths.pop();
paths.pop();
return this.createFolder(paths.join('/') + '/', options);
}
}
else if (resourceAddress.endsWith('/')) {
paths.pop();
paths.pop();
return this.createFolder(paths.join('/') + '/', options);
}
else {
paths.pop();
return this.createFile(paths.join('/') + '/', options);
}
};
exports.createFolder = async function (folderAddress, options) {
var _a;
const request = {
method: 'POST',
headers: {
Slug: (_a = options === null || options === void 0 ? void 0 : options.name) !== null && _a !== void 0 ? _a : 'Untitled Folder',
Link: '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"',
'Content-Type': 'text/turtle',
},
};
const res = (await this.fetcher._fetch(folderAddress, request));
const location = res.headers.get('Location');
if (location && res.status < 304) {
const parsedUrl = url_1.default.parse(folderAddress);
const rootUrl = `${parsedUrl.protocol}//${parsedUrl.host}`;
await this.addToIndex({ url: rootUrl + location, types: [solid_namespace_1.default().ldp('Container')] }, { force: true });
}
return res;
};
exports.createFile = async function (fileAddress, options = {}) {
var _a, _b, _c, _d, _e;
const stringContent = options.contents;
const blobContent = options.contents;
const anyContent = options.contents;
const body = (_a = stringContent !== null && stringContent !== void 0 ? stringContent : blobContent) !== null && _a !== void 0 ? _a : bodyFromContent(anyContent);
if (options.name) {
options.contentType = (_c = (_b = options.contentType) !== null && _b !== void 0 ? _b : mime_1.default.getType(options.name)) !== null && _c !== void 0 ? _c : 'text/turtle';
const fileExtension = (options.contentType === 'image/jpeg' ||
options.contentType === 'application/octet-stream') &&
options.name &&
options.name.split('.')[1] === 'jpg'
? 'jpg'
: mime_1.default.getExtension(options.contentType);
fileAddress = `${fileAddress}${options.name.split('.')[0]}.${fileExtension}`;
}
const request = {
contentType: options.contentType,
data: body,
};
const res = await this.fetcher.webOperation('PUT', fileAddress, request);
const location = (_d = res.headers.get('Location')) !== null && _d !== void 0 ? _d : url_1.default.parse(fileAddress).pathname;
if (location && res.status < 300) {
const parsedUrl = url_1.default.parse(fileAddress);
const rootUrl = `${parsedUrl.protocol}//${parsedUrl.host}`;
await this.addToIndex({
url: rootUrl + location,
types: [
types((_e = options.contentType) !== null && _e !== void 0 ? _e : 'text/plain' + '#Resource'),
solid_namespace_1.default().ldp('Resource'),
],
});
}
return await res;
};
exports.createIfNotExist = function (resourceAddress, options = { headers: {} }) {
return this.fetcher
._fetch(resourceAddress, { method: 'HEAD', headers: options.headers })
.then((res) => {
if (res.status === 200) {
return;
}
else {
return this.create(resourceAddress, options);
}
})
.catch((err) => {
if (err.response.status === 404)
return this.create(resourceAddress, options);
});
};
function bodyFromContent(content) {
if (content) {
if (Array.isArray(content)) {
if (content.every((el) => {
return el.constructor.name === 'Statement';
})) {
let bodyString = '';
content.forEach((st) => {
bodyString += st.toNT() + '\n';
});
return bodyString;
}
}
else {
return content;
}
}
else {
return '';
}
}
//# sourceMappingURL=create.js.map