1 | import {cached, defineAssoc} from "./decorators.js";
|
2 | import {lib, Collection, RallyBase, FileTooLargeError} from "./rally-tools.js";
|
3 |
|
4 | class 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 |
|
47 | defineAssoc(File, "id", "data.id");
|
48 | defineAssoc(File, "name", "data.attributes.label");
|
49 | defineAssoc(File, "contentLink", "data.links.content");
|
50 | defineAssoc(File, "selfLink", "data.links.self");
|
51 | defineAssoc(File, "label", "data.attributes.label");
|
52 | defineAssoc(File, "md5", "data.attributes.md5");
|
53 | defineAssoc(File, "sha512", "data.attributes.sha512");
|
54 | defineAssoc(File, "tags", "data.attributes.tagList");
|
55 | defineAssoc(File, "instances", "data.attributes.instances");
|
56 | File.endpoint = null
|
57 |
|
58 | export default File;
|