-
Notifications
You must be signed in to change notification settings - Fork 593
/
fileAttribute.js
81 lines (68 loc) · 1.91 KB
/
fileAttribute.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
// @flow
import _ from "lodash";
import { EntityAttribute, type EntityAttributesContainer } from "webiny-entity";
import type { Storage } from "webiny-file-storage";
import type { File } from "./../entities";
class FileAttribute extends EntityAttribute {
storage: Storage;
storageFolder: string;
tags: Array<string>;
constructor(name: string, attributesContainer: EntityAttributesContainer, entity: Class<File>) {
super(name, attributesContainer, entity);
this.tags = [];
this.auto.delete = {
enabled: true,
options: { permanent: true }
};
}
/**
* Set tags that will always be assigned to the file
*
* @param tags
*
* @return this
*/
setTags(tags: Array<string> = []): FileAttribute {
this.tags = tags;
return this;
}
/**
* Set storage to use with this attribute
*
* @param {Storage} storage
*
* @return this
*/
setStorage(storage: Storage): FileAttribute {
this.storage = storage;
return this;
}
/**
* Set folder in which the file will be stored (relative to the root of your storage)
*
* @param {string} folder
*
* @return this
*/
setFolder(folder: string): FileAttribute {
this.storageFolder = folder;
return this;
}
async getValue() {
let value = await EntityAttribute.prototype.getValue.call(this);
if (value instanceof this.getEntityClass()) {
if (this.storage) {
value.setStorage(this.storage).setStorageFolder(this.storageFolder);
}
}
return value;
}
// $FlowFixMe
setValue(value) {
if (Array.isArray(_.get(value, "tags"))) {
value.tags = _.uniqWith([...this.tags, ...(value.tags || [])], _.isEqual);
}
super.setValue(value);
}
}
export default FileAttribute;