Skip to content

Commit a6abfa2

Browse files
committed
Merge branch 'develop'
2 parents 53eec14 + 82cf19a commit a6abfa2

8 files changed

+123
-55
lines changed

docs/changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 2.4.2
4+
5+
* fix: issue [#2](https://github.com/batosai/adonis-attachment/issues/2), cannot read properties of undefined (reading 'getConfig')
6+
* fix: delete old file after option rename changed
7+
38
## 2.4.1
49

510
* fix: typo error mimetype -> mimeType

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jrmc/adonis-attachment",
3-
"version": "2.4.1",
3+
"version": "2.4.2",
44
"type": "module",
55
"description": "Turn any field on your Lucid model to an attachment data type",
66
"engines": {

src/attachment_manager.ts

+29-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export class AttachmentManager {
5050
}
5151
})
5252

53-
return new Attachment(this.#drive, attributes)
53+
const attachment = new Attachment(this.#drive, attributes)
54+
return this.#configureAttachment(attachment)
5455
}
5556

5657
async createFromFile(file: MultipartFile) {
@@ -65,7 +66,8 @@ export class AttachmentManager {
6566
throw new errors.ENOENT()
6667
}
6768

68-
return new Attachment(this.#drive, attributes, file.tmpPath)
69+
const attachment = new Attachment(this.#drive, attributes, file.tmpPath)
70+
return this.#configureAttachment(attachment)
6971
}
7072

7173
async createFromBuffer(buffer: Buffer, name?: string) {
@@ -75,7 +77,8 @@ export class AttachmentManager {
7577

7678
const attributes = await createAttachmentAttributes(buffer, name)
7779

78-
return new Attachment(this.#drive, attributes, buffer)
80+
const attachment = new Attachment(this.#drive, attributes, buffer)
81+
return this.#configureAttachment(attachment)
7982
}
8083

8184
async createFromBase64(data: string, name?: string) {
@@ -147,9 +150,7 @@ export class AttachmentManager {
147150

148151
async delete(attachment: AttachmentBase) {
149152
if (attachment.path) {
150-
const filePath = attachment.path
151-
152-
await attachment.getDisk().delete(filePath)
153+
await attachment.getDisk().delete(attachment.path)
153154

154155
if (attachment instanceof Attachment) {
155156
if (attachment.variants) {
@@ -158,5 +159,27 @@ export class AttachmentManager {
158159
}
159160
}
160161
}
162+
163+
if (attachment.originalPath) {
164+
await attachment.getDisk().delete(attachment.originalPath)
165+
}
166+
}
167+
168+
// private methods
169+
170+
#configureAttachment(attachment: AttachmentType) {
171+
if (this.#config.meta !== undefined) {
172+
attachment.setOptions({ meta: this.#config.meta })
173+
}
174+
175+
if (this.#config.rename !== undefined) {
176+
attachment.setOptions({ rename: this.#config.rename })
177+
}
178+
179+
if (this.#config.preComputeUrl !== undefined) {
180+
attachment.setOptions({ preComputeUrl: this.#config.preComputeUrl })
181+
}
182+
183+
return attachment
161184
}
162185
}

src/attachments/attachment.ts

+20-9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ export class Attachment extends AttachmentBase implements AttachmentInterface {
3737
}
3838
}
3939

40+
/**
41+
* Getters
42+
*/
43+
44+
get name() {
45+
if (this.options && this.options.rename === false) {
46+
return this.originalName
47+
}
48+
49+
return super.name
50+
}
51+
52+
/**
53+
* Methods
54+
*/
55+
4056
async createVariant(key: string, input: Input): Promise<Variant> {
4157
const attributes = {
4258
...(await createAttachmentAttributes(input)),
@@ -101,15 +117,6 @@ export class Attachment extends AttachmentBase implements AttachmentInterface {
101117
...options,
102118
}
103119

104-
if (!this.path) {
105-
if (!this.options.rename) {
106-
this.name = this.originalName
107-
}
108-
109-
this.folder = this.options!.folder!
110-
this.path = path.join(this.folder, this.name)
111-
}
112-
113120
if (this.variants) {
114121
this.variants.forEach((v) => {
115122
v.setOptions({
@@ -122,6 +129,10 @@ export class Attachment extends AttachmentBase implements AttachmentInterface {
122129
return this
123130
}
124131

132+
/**
133+
*
134+
*/
135+
125136
toObject(): AttachmentAttributes {
126137
const variants = this.variants?.map((v) => v.toObject())
127138

src/attachments/attachment_base.ts

+38-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
} from '../types/attachment.js'
1414
import type { Exif, Input } from '../types/input.js'
1515

16+
import path from 'node:path'
1617
import { cuid } from '@adonisjs/core/helpers'
1718
import { defaultOptionsDecorator } from '../utils/default_values.js'
1819

@@ -21,13 +22,14 @@ export class AttachmentBase implements AttachmentBaseInterface {
2122

2223
input?: Input
2324

24-
name: string
25+
#name: string
26+
#folder?: string
27+
2528
size: number
2629
extname: string
2730
mimeType: string
2831
meta?: Exif
29-
folder?: string
30-
path?: string
32+
originalPath?: string
3133
url?: string
3234

3335
options?: LucidOptions
@@ -39,20 +41,42 @@ export class AttachmentBase implements AttachmentBaseInterface {
3941
this.meta = attributes.meta
4042
this.extname = attributes.extname
4143
this.mimeType = attributes.mimeType
42-
this.folder = attributes.folder
43-
this.path = attributes.path
44+
this.originalPath = attributes.path
4445

45-
this.options = defaultOptionsDecorator
46+
this.#folder = attributes.folder
47+
if (attributes.name) {
48+
this.#name = attributes.name
49+
} else {
50+
this.#name = `${cuid()}.${this.extname}`
51+
}
4652

53+
this.options = defaultOptionsDecorator
4754
this.drive = drive
55+
}
4856

49-
if (attributes.name) {
50-
this.name = attributes.name
51-
} else {
52-
this.name = `${cuid()}.${this.extname}`
57+
/**
58+
* Getters
59+
*/
60+
61+
get name(): string {
62+
return this.#name
63+
}
64+
65+
get folder(): string | undefined {
66+
if (this.options) {
67+
return this.options?.folder
5368
}
69+
return this.#folder
5470
}
5571

72+
get path(): string {
73+
return path.join(this.folder!, this.name)
74+
}
75+
76+
/**
77+
* Methods
78+
*/
79+
5680
getDisk() {
5781
return this.drive.use(this.options?.disk)
5882
}
@@ -73,6 +97,10 @@ export class AttachmentBase implements AttachmentBaseInterface {
7397
return this
7498
}
7599

100+
/**
101+
*
102+
*/
103+
76104
toObject(): AttachmentBaseAttributes {
77105
return {
78106
name: this.name,

src/attachments/variant_attachment.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,35 @@ import type { DriveService } from '@adonisjs/drive/types'
99
import type { VariantAttributes, Variant as VariantInterface } from '../types/attachment.js'
1010
import type { Input } from '../types/input.js'
1111

12-
import path from 'node:path'
1312
import { AttachmentBase } from './attachment_base.js'
1413

1514
export class Variant extends AttachmentBase implements VariantInterface {
1615
key: string
17-
folder: string
16+
#folder: string
1817

1918
constructor(drive: DriveService, attributes: VariantAttributes, input?: Input) {
2019
super(drive, attributes, input)
2120

2221
this.key = attributes.key
23-
this.folder = attributes.folder!
24-
this.path = path.join(this.folder, this.name)
22+
this.#folder = attributes.folder!
2523
}
2624

25+
/**
26+
* Getters
27+
*/
28+
29+
get folder(): string {
30+
return this.#folder
31+
}
32+
33+
/**
34+
*
35+
*/
36+
2737
toObject(): VariantAttributes {
2838
return {
2939
key: this.key,
30-
folder: this.folder,
40+
folder: this.folder!,
3141
name: this.name,
3242
...super.toObject(),
3343
}

src/decorators/attachment.ts

+11-22
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,6 @@ export const attachment = (options?: LucidOptions) => {
1818
target[optionsSym] = {}
1919
}
2020

21-
const defaultConfig = attachmentManager.getConfig()
22-
const defaultOptions = {
23-
meta: defaultConfig.meta !== undefined ? defaultConfig.meta : defaultOptionsDecorator.meta,
24-
rename:
25-
defaultConfig.rename !== undefined ? defaultConfig.rename : defaultOptionsDecorator.rename,
26-
preComputeUrl:
27-
defaultConfig.preComputeUrl !== undefined
28-
? defaultConfig.preComputeUrl
29-
: defaultOptionsDecorator.preComputeUrl,
30-
}
31-
32-
if (!options || options?.meta === undefined) {
33-
options!.meta = defaultOptions.meta
34-
}
35-
if (!options || options?.rename === undefined) {
36-
options!.rename = defaultOptions.rename
37-
}
38-
if (!options || options?.preComputeUrl === undefined) {
39-
options!.preComputeUrl = defaultOptions.preComputeUrl
40-
}
41-
4221
target[optionsSym][attributeName] = options
4322

4423
const Model = target.constructor as LucidModel
@@ -53,7 +32,17 @@ export const attachment = (options?: LucidOptions) => {
5332
consume: (value) => {
5433
if (value) {
5534
const attachment = attachmentManager.createFromDbResponse(value)
56-
attachment?.setOptions({ disk, folder, variants, meta, rename })
35+
attachment?.setOptions({ disk, folder, variants })
36+
37+
if (options && options?.meta !== undefined) {
38+
attachment?.setOptions({ meta: options!.meta })
39+
}
40+
if (options && options?.rename !== undefined) {
41+
attachment?.setOptions({ rename: options!.rename })
42+
}
43+
if (options && options?.preComputeUrl !== undefined) {
44+
attachment?.setOptions({ preComputeUrl: options!.preComputeUrl })
45+
}
5746
return attachment
5847
} else {
5948
return null

src/types/attachment.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ export type AttachmentBase = {
1616
input?: Input
1717

1818
name: string
19+
folder?: string
20+
path?: string
21+
1922
size: number
2023
extname: string
2124
mimeType: string
2225
meta?: Exif
23-
folder?: string
24-
path?: string
26+
originalPath?: string
2527
url?: string
2628

2729
options?: LucidOptions

0 commit comments

Comments
 (0)