adonis-responsive-attachment
Advanced tools
Comparing version 1.5.1 to 1.5.2
@@ -150,5 +150,5 @@ /// <reference types="@adonisjs/drive/build/adonis-typings" /> | ||
new (attributes: ImageAttributes, file?: MultipartFileContract): ResponsiveAttachmentContract; | ||
fromFile(file: MultipartFileContract): Promise<ResponsiveAttachmentContract>; | ||
fromFile(file: MultipartFileContract, fileName?: string): Promise<ResponsiveAttachmentContract>; | ||
fromDbResponse(response: string): ResponsiveAttachmentContract; | ||
fromBuffer(buffer: Buffer): Promise<ResponsiveAttachmentContract>; | ||
fromBuffer(buffer: Buffer, fileName?: string): Promise<ResponsiveAttachmentContract>; | ||
getDrive(): DriveManagerContract; | ||
@@ -155,0 +155,0 @@ setDrive(drive: DriveManagerContract): void; |
@@ -28,3 +28,3 @@ /// <reference path="../../adonis-typings/index.d.ts" /> | ||
*/ | ||
static fromFile(file: MultipartFileContract): Promise<ResponsiveAttachmentContract>; | ||
static fromFile(file: MultipartFileContract, fileName?: string): Promise<ResponsiveAttachmentContract>; | ||
/** | ||
@@ -31,0 +31,0 @@ * Create attachment instance from the bodyparser via a buffer |
@@ -44,3 +44,3 @@ "use strict"; | ||
*/ | ||
static async fromFile(file) { | ||
static async fromFile(file, fileName) { | ||
if (!file) { | ||
@@ -57,4 +57,3 @@ throw new SyntaxError('You should provide a non-falsy value'); | ||
const buffer = await (0, promises_1.readFile)(file.tmpPath); | ||
let fileNameArray = file.clientName.split('.'); | ||
const fileName = fileNameArray.slice(0, fileNameArray.length - 1).join('.'); | ||
const computedFileName = fileName ? fileName : file.fieldName; | ||
const attributes = { | ||
@@ -64,3 +63,3 @@ extname: file.extname, | ||
size: file.size, | ||
fileName, | ||
fileName: computedFileName.replace(/[^\d\w]+/g, '_').toLowerCase(), | ||
}; | ||
@@ -94,3 +93,3 @@ return new ResponsiveAttachment(attributes, buffer); | ||
size: buffer.length, | ||
fileName: name, | ||
fileName: name?.replace(/[^\d\w]+/g, '_')?.toLowerCase() ?? '', | ||
}; | ||
@@ -141,3 +140,2 @@ return resolve(new ResponsiveAttachment(attributes, buffer)); | ||
this.isLocal = !!this.buffer; | ||
this.fileName = attributes.fileName; | ||
} | ||
@@ -144,0 +142,0 @@ get attributes() { |
{ | ||
"name": "adonis-responsive-attachment", | ||
"version": "1.5.1", | ||
"version": "1.5.2", | ||
"description": "Generate and persist optimised and responsive breakpoint images on the fly in your AdonisJS application.", | ||
@@ -5,0 +5,0 @@ "main": "build/providers/ResponsiveAttachmentProvider.js", |
@@ -129,2 +129,4 @@ # Adonis Responsive Attachment | ||
Both methods allow you to provide an optional file name as the second parameter. This replaces the use of input file name from the uploaded file. The add-on will replace all non-numeric and non-alphabet characters in the file name with the underscore character. | ||
The example below shows the use of the `fromFile` static method. | ||
@@ -140,3 +142,3 @@ | ||
post.coverImage = coverImage ? await ResponsiveAttachment.fromFile(coverImage) : null | ||
post.coverImage = coverImage ? await ResponsiveAttachment.fromFile(coverImage, 'My Great Name') : null | ||
await post.save() | ||
@@ -156,3 +158,3 @@ } | ||
const user = new User() | ||
user.avatar = await ResponsiveAttachment.fromBuffer(buffer) | ||
user.avatar = await ResponsiveAttachment.fromBuffer(buffer, 'Awesome File Name') | ||
await user.save() | ||
@@ -159,0 +161,0 @@ } |
86257
183
762
1464