forked from aaronmars/martian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.js
81 lines (74 loc) · 3.61 KB
/
file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { Plug } from 'mindtouch-http.js/plug.js';
import { ProgressPlug } from 'mindtouch-http.js/progressPlug.js';
import { Settings } from './lib/settings.js';
import { utility } from './lib/utility.js';
import { modelParser } from './lib/modelParser.js';
import { fileModel } from './models/file.model.js';
import { fileRevisionsModel } from './models/fileRevisions.model.js';
/**
* A class for working with file attachments within the MindTouch site.
*/
export class File {
/**
* Construct a new File object.
* @param {Number} id - The resource ID of the file.
* @param {Settings} [settings] - The {@link Settings} information to use in construction. If not supplied, the default settings are used.
*/
constructor(id, settings = new Settings()) {
this._id = id;
this._settings = settings;
this._plug = new Plug(settings.host, settings.plugConfig).at('@api', 'deki', 'files', id);
this._progressPlug = new ProgressPlug(settings.host, settings.plugConfig).at('@api', 'deki', 'files', id);
}
/**
* Get the file attachment information.
* @returns {Promise.<fileModel>} - A Promise that, when resolved, yields a {@link fileModel} containing the attachment information.
*/
getInfo() {
let fileModelParser = modelParser.createParser(fileModel);
return this._plug.at('info').get().then((r) => r.json()).then(fileModelParser);
}
/**
* Get the revision list of the file attachment.
* @returns {Promise.<fileRevisionsModel>} - A Promise that, when resolved, yields a {@link fileRevisionsModel} containing the revision listing.
*/
getRevisions() {
return this._plug.at('revisions').get().then((r) => r.json()).then(modelParser.createParser(fileRevisionsModel));
}
/**
* Set the description for the file.
* @param {String} description - The new file description.
* @returns {Promise.<fileModel>} - A Promise that, when resolved, yields a {@link fileModel} containing the file information.
*/
setDescription(description) {
let fileModelParser = modelParser.createParser(fileModel);
return this._plug.at('description').put(description, utility.textRequestType).then((r) => r.json()).then(fileModelParser);
}
/**
* Delete the file from the MindTouch site.
* @returns {Promise} - A Promise that, when resolved, indicates a successful file deletion.
*/
delete() {
return this._plug.delete();
}
/**
* Upload a new file to serve as a revision in place of the current file.
* @param { File } file - The file object to upload.
* @param { String } filename - The filename of the new revision.
* @param { function } progress - A function that is called to indicate upload progress before the upload is complete.
*/
addRevision(file, { name = file.name, size = file.size, type = file.type, progress = null } = {}) {
if(progress !== null) {
const progressInfo = { callback: progress, size };
return this._progressPlug.at(utility.getResourceId(name)).put(file, type, progressInfo).then((r) => JSON.parse(r.responseText)).then(modelParser.createParser(fileModel));
}
return this._plug.withHeader('Content-Length', size).at(utility.getResourceId(name)).put(file, type).then((r) => r.json()).then(modelParser.createParser(fileModel));
}
}
export class FileDraft extends File {
constructor(id, settings = new Settings()) {
super(id, settings);
this._plug = this._plug.withParam('draft', 'true');
this._progressPlug = this._progressPlug.withParam('draft', 'true');
}
}