magister.js
Version:
A JavaScript implementation of the Magister 6 API
198 lines (157 loc) • 4.22 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _url = _interopRequireDefault(require("url"));
var _lodash = _interopRequireDefault(require("lodash"));
var _magisterThing = _interopRequireDefault(require("./magisterThing"));
var _person = _interopRequireDefault(require("./person"));
var _util = require("./util");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class File extends _magisterThing.default {
/**
* @private
* @param {Magister} magister
* @param {FileFolder} fileFolder
* @param {Object} raw
*/
constructor(magister, fileFolder, raw) {
super(magister);
/**
* @type {String}
* @readonly
*/
this.id = (0, _util.toString)(raw.Id);
/**
* @type {Number}
* @readonly
*/
this.type = raw.BronSoort; // REVIEW: string? And there is a difference in Magister between the Type and BronSoort
/**
* @type {String}
* @readonly
*/
this.name = raw.Naam;
/**
* @type {String}
* @readonly
*/
this.uri = raw.Uri;
/**
* @type {Number}
* @readonly
*/
this.size = raw.Grootte; // REVIEW
/**
* @type {Number}
* @readonly
*/
this.rights = raw.Privilege;
/**
* @type {String}
* @readonly
*/
this.mime = raw.ContentType || 'application/octet-stream';
/**
* @type {Date}
* @readonly
*/
this.changedDate = (0, _util.parseDate)(raw.GewijzigdOp);
/**
* @type {Date}
* @readonly
*/
this.creationDate = (0, _util.parseDate)(raw.GemaaktOp || raw.Datum);
/**
* @type {Person}
* @readonly
*/
this.addedBy = new _person.default(magister, {
Naam: raw.GeplaatstDoor
});
/**
* @type {String}
* @readonly
*/
this.fileBlobId = (0, _util.toString)(raw.FileBlobId);
/**
* @type {FileFolder}
* @readonly
*/
this.fileFolder = fileFolder;
/**
* @type {String}
* @readonly
*/
this.uniqueId = raw.UniqueId;
/**
* @type {String}
* @readonly
*/
this.referenceId = (0, _util.toString)(raw.Referentie);
const selfUrl = _lodash.default.find(raw.Links, {
Rel: 'Self'
});
const contentUrl = _lodash.default.find(raw.Links, {
Rel: 'Contents'
});
const getUrl = link => !link ? null : _url.default.resolve(magister.school.url, link.Href);
/**
* @type {String|null}
* @readonly
* @private
*/
this._selfUrl = getUrl(selfUrl);
/**
* @type {String}
* @readonly
* @private
*/
this._downloadUrl = getUrl(contentUrl || selfUrl);
}
/**
* Opens a stream to the current file
* @returns {Promise<Stream>}
*/
download() {
return this._magister._privileges.needs('bronnen', 'read').then(() => this._magister.http.get(this._downloadUrl)).then(res => res.body);
}
/**
* Removes the current file permanently
* @returns {Promise<undefined>}
*/
remove() {
return this._magister._privileges.needs('bronnen', 'delete').then(() => this._magister.http.delete(this._selfUrl)).then(() => undefined); // throw away the useless result from magister. (current object)
}
/**
* Update the server to reflect the changes made on the properties of this
* File instance.
* @returns {Promise<undefined>}
*/
saveChanges() {
return this._magister._privileges.needs('bronnen', 'update').then(() => this._magister.http.put(this._selfUrl, this._toMagister())).then(() => undefined);
}
/**
* @private
* @returns {Object}
*/
_toMagister() {
const toNumberSafe = val => val == null ? val : parseInt(val, 10);
return {
Id: parseInt(this.id, 10),
BronSoort: this.type,
Naam: this.name,
Uri: this.uri,
Grootte: this.size,
Privilege: this.rights,
ContentType: this.mime,
FileBlobId: toNumberSafe(this.fileBlobId),
ParentId: this.fileFolder.id,
UniqueId: this.uniqueId,
Referentie: toNumberSafe(this.referenceId)
};
}
}
var _default = File;
exports.default = _default;