Backporting Attachments (#1817)

This commit is contained in:
SpaceEEC
2017-08-22 00:39:27 +02:00
committed by Crawl
parent f7664b01a2
commit 0b22d9a774
7 changed files with 208 additions and 36 deletions

View File

@@ -234,6 +234,33 @@ class ClientDataResolver {
return Promise.reject(new TypeError('The resource must be a string or Buffer.'));
}
/**
* @external Stream
* @see {@link https://nodejs.org/api/stream.html}
*/
/**
* Converts a Stream to a Buffer.
* @param {Stream} resource The stream to convert
* @returns {Promise<Buffer>}
*/
resolveFile(resource) {
return resource ? this.resolveBuffer(resource)
.catch(() => {
if (resource.pipe && typeof resource.pipe === 'function') {
return new Promise((resolve, reject) => {
const buffers = [];
resource.once('error', reject);
resource.on('data', data => buffers.push(data));
resource.once('end', () => resolve(Buffer.concat(buffers)));
});
} else {
throw new TypeError('The resource must be a string, Buffer or a valid file stream.');
}
}) :
Promise.reject(new TypeError('The resource must be a string, Buffer or a valid file stream.'));
}
/**
* Data that can be resolved to give an emoji identifier. This can be:
* * The unicode representation of an emoji