UNPKG

1.8 kBJavaScriptView Raw
1import {cached, defineAssoc} from "./decorators.js";
2import {lib, Collection, RallyBase, FileTooLargeError} from "./rally-tools.js";
3
4class File extends RallyBase{
5 constructor({data, remote, included, parent}){
6 super();
7 this.data = data;
8 this.meta = {};
9 this.remote = remote;
10 this.parentAsset = parent;
11 }
12
13 chalkPrint(pad=false){
14 let id = String("F-" + (this.remote && this.remote + "-" + this.id || "LOCAL"))
15 if(pad) id = id.padStart(15);
16 return chalk`{green ${id}}: {blue ${this.data.attributes ? this.name : "(lite asset)"}}`;
17 }
18
19 canBeDownloaded(){
20 return this.sizeGB <= .2;
21 }
22
23 async getContent(force = false){
24 if(!this.canBeDownloaded() && !force){
25 throw new FileTooLargeError(this);
26 }
27
28 return lib.makeAPIRequest({
29 env: this.remote, fullPath: this.contentLink
30 });
31 }
32 async delete(remove = true){
33 return lib.makeAPIRequest({
34 env: this.remote, fullPath: this.selfLink,
35 method: "DELETE",
36 });
37 }
38 get size(){
39 return Object.values(this.data.attributes.instances)[0].size
40 }
41
42 get sizeGB(){
43 return Math.round(this.size / 1024 / 1024 / 1024 * 10) / 10;
44 }
45}
46
47defineAssoc(File, "id", "data.id");
48defineAssoc(File, "name", "data.attributes.label");
49defineAssoc(File, "contentLink", "data.links.content");
50defineAssoc(File, "selfLink", "data.links.self");
51defineAssoc(File, "label", "data.attributes.label");
52defineAssoc(File, "md5", "data.attributes.md5");
53defineAssoc(File, "sha512", "data.attributes.sha512");
54defineAssoc(File, "tags", "data.attributes.tagList");
55defineAssoc(File, "instances", "data.attributes.instances");
56File.endpoint = null
57
58export default File;