-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattachment.js
More file actions
39 lines (35 loc) · 918 Bytes
/
attachment.js
File metadata and controls
39 lines (35 loc) · 918 Bytes
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
import Link from './link.js'
/**
* Represents an attachment.
* @class
*/
export default class Attachment extends Link {
#filename = null
constructor(url, filename) {
super(url)
this.setFilename(filename)
}
toJSON() {
return {
...super.toJSON(),
filename: this.#filename,
type: 'attachment',
}
}
/**
* @param {String} filename - The filename of the attachment.
* @returns {Attachment} The updated attachment instance.
* @throws {TypeError} If the filename is not valid.
*/
setFilename(filename) {
if (typeof filename !== 'string' || filename.trim() === '') {
throw new TypeError('Filename must be a non-empty string.')
}
const illegalChars = /[\\/:*?"<>|]/g
if (illegalChars.test(filename)) {
throw new TypeError(`Filename contains invalid characters: ${filename}`)
}
this.#filename = filename
return this
}
}