refactor(Attachment): Merge MessageAttachment with Attachment (#1894)

* refactor(Attachment): Merge MessageAttachment with Attachment

* refactor(Attachment): Rename setup to _patch for consistency

* refactor(MessageAttachment): Global rename of Attachment class
This commit is contained in:
Robin B
2017-09-06 23:12:20 +02:00
committed by Crawl
parent 694f78cd43
commit d6b276bc29
7 changed files with 99 additions and 119 deletions

View File

@@ -1,43 +1,88 @@
/**
* Represents an attachment in a message.
* @param {BufferResolvable|Stream} file The file
* @param {string} [name] The name of the file, if any
*/
class MessageAttachment {
constructor(message, data) {
/**
* The client that instantiated this MessageAttachment
* @name MessageAttachment#client
* @type {Client}
* @readonly
*/
Object.defineProperty(this, 'client', { value: message.client });
/**
* The message this attachment is part of
* @type {Message}
*/
this.message = message;
this.setup(data);
constructor(file, name, data) {
this.file = null;
if (data) this._patch(data);
if (name) this.setAttachment(file, name);
else this._attach(file);
}
setup(data) {
/**
* The name of the file
* @type {?string}
* @readonly
*/
get name() {
return this.file.name;
}
/**
* The file
* @type {?BufferResolvable|Stream}
* @readonly
*/
get attachment() {
return this.file.attachment;
}
/**
* Set the file of this attachment.
* @param {BufferResolvable|Stream} file The file
* @param {string} name The name of the file
* @returns {MessageAttachment} This attachment
*/
setAttachment(file, name) {
this.file = { attachment: file, name };
return this;
}
/**
* Set the file of this attachment.
* @param {BufferResolvable|Stream} attachment The file
* @returns {MessageAttachment} This attachment
*/
setFile(attachment) {
this.file = { attachment };
return this;
}
/**
* Set the name of this attachment.
* @param {string} name The name of the image
* @returns {MessageAttachment} This attachment
*/
setName(name) {
this.file.name = name;
return this;
}
/**
* Set the file of this attachment.
* @param {BufferResolvable|Stream} file The file
* @param {string} name The name of the file
* @private
*/
_attach(file, name) {
if (typeof file === 'string') this.file = file;
else this.setAttachment(file, name);
}
_patch(data) {
/**
* The ID of this attachment
* @type {Snowflake}
*/
this.id = data.id;
/**
* The file name of this attachment
* @type {string}
*/
this.filename = data.filename;
/**
* The size of this attachment in bytes
* @type {number}
*/
this.filesize = data.size;
this.size = data.size;
/**
* The URL to this attachment