Skip to content

Commit 82cf19a

Browse files
committed
fix(issue #2): refacto options manager
1 parent 488d647 commit 82cf19a

7 files changed

+92
-34
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

+9-7
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,7 @@ export class AttachmentManager {
150150

151151
async delete(attachment: AttachmentBase) {
152152
if (attachment.path) {
153-
const filePath = attachment.path
154-
155-
await attachment.getDisk().delete(filePath)
153+
await attachment.getDisk().delete(attachment.path)
156154

157155
if (attachment instanceof Attachment) {
158156
if (attachment.variants) {
@@ -161,6 +159,10 @@ export class AttachmentManager {
161159
}
162160
}
163161
}
162+
163+
if (attachment.originalPath) {
164+
await attachment.getDisk().delete(attachment.originalPath)
165+
}
164166
}
165167

166168
// private methods
@@ -170,12 +172,12 @@ export class AttachmentManager {
170172
attachment.setOptions({ meta: this.#config.meta })
171173
}
172174

173-
if (this.#config.preComputeUrl !== undefined) {
174-
attachment.setOptions({ preComputeUrl: this.#config.preComputeUrl })
175+
if (this.#config.rename !== undefined) {
176+
attachment.setOptions({ rename: this.#config.rename })
175177
}
176178

177-
if (this.#config.meta !== undefined) {
178-
attachment.setOptions({ meta: this.#config.meta })
179+
if (this.#config.preComputeUrl !== undefined) {
180+
attachment.setOptions({ preComputeUrl: this.#config.preComputeUrl })
179181
}
180182

181183
return attachment

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/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)