1
- import type { CollectionSlug , GlobalSlug , Payload , PayloadRequest } from 'payload'
2
-
3
- import fs from 'fs'
4
- import path from 'path'
5
- import { fileURLToPath } from 'url'
1
+ import type { CollectionSlug , GlobalSlug , Payload , PayloadRequest , File } from 'payload'
6
2
7
3
import { contactForm as contactFormData } from './contact-form'
8
4
import { contact as contactPageData } from './contact-page'
@@ -13,9 +9,6 @@ import { post1 } from './post-1'
13
9
import { post2 } from './post-2'
14
10
import { post3 } from './post-3'
15
11
16
- const filename = fileURLToPath ( import . meta. url )
17
- const dirname = path . dirname ( filename )
18
-
19
12
const collections : CollectionSlug [ ] = [
20
13
'categories' ,
21
14
'media' ,
@@ -46,12 +39,6 @@ export const seed = async ({
46
39
// the custom `/api/seed` endpoint does not
47
40
48
41
payload . logger . info ( `— Clearing media...` )
49
-
50
- const mediaDir = path . resolve ( dirname , '../../public/media' )
51
- if ( fs . existsSync ( mediaDir ) ) {
52
- fs . rmdirSync ( mediaDir , { recursive : true } )
53
- }
54
-
55
42
payload . logger . info ( `— Clearing collections and globals...` )
56
43
57
44
// clear the database
@@ -110,28 +97,65 @@ export const seed = async ({
110
97
let demoAuthorID : number | string = demoAuthor . id
111
98
112
99
payload . logger . info ( `— Seeding media...` )
100
+ const [ image1Buffer , image2Buffer , image3Buffer , hero1Buffer ] = await Promise . all ( [
101
+ fetchFileByURL (
102
+ 'https://raw.githubusercontent.com/payloadcms/payload/refs/heads/main/templates/website/src/endpoints/seed/image-post1.webp' ,
103
+ ) ,
104
+ fetchFileByURL (
105
+ 'https://raw.githubusercontent.com/payloadcms/payload/refs/heads/main/templates/website/src/endpoints/seed/image-post2.webp' ,
106
+ ) ,
107
+ fetchFileByURL (
108
+ 'https://raw.githubusercontent.com/payloadcms/payload/refs/heads/main/templates/website/src/endpoints/seed/image-post3.webp' ,
109
+ ) ,
110
+ fetchFileByURL (
111
+ 'https://raw.githubusercontent.com/payloadcms/payload/refs/heads/main/templates/website/src/endpoints/seed/image-hero1.webp' ,
112
+ ) ,
113
+ ] )
114
+ // Log all but not the buffer
115
+ req . payload . logger . info ( {
116
+ image1Buffer : {
117
+ name : image1Buffer . name ,
118
+ mimetype : image1Buffer . mimetype ,
119
+ size : image1Buffer . size ,
120
+ } ,
121
+ image2Buffer : {
122
+ name : image2Buffer . name ,
123
+ mimetype : image2Buffer . mimetype ,
124
+ size : image2Buffer . size ,
125
+ } ,
126
+ image3Buffer : {
127
+ name : image3Buffer . name ,
128
+ mimetype : image3Buffer . mimetype ,
129
+ size : image3Buffer . size ,
130
+ } ,
131
+ hero1Buffer : {
132
+ name : hero1Buffer . name ,
133
+ mimetype : hero1Buffer . mimetype ,
134
+ size : hero1Buffer . size ,
135
+ } ,
136
+ } )
113
137
const image1Doc = await payload . create ( {
114
138
collection : 'media' ,
115
139
data : image1 ,
116
- filePath : path . resolve ( dirname , 'image-post1.webp' ) ,
140
+ file : image1Buffer ,
117
141
req,
118
142
} )
119
143
const image2Doc = await payload . create ( {
120
144
collection : 'media' ,
121
145
data : image2 ,
122
- filePath : path . resolve ( dirname , 'image-post2.webp' ) ,
146
+ file : image2Buffer ,
123
147
req,
124
148
} )
125
149
const image3Doc = await payload . create ( {
126
150
collection : 'media' ,
127
151
data : image2 ,
128
- filePath : path . resolve ( dirname , 'image-post3.webp' ) ,
152
+ file : image3Buffer ,
129
153
req,
130
154
} )
131
155
const imageHomeDoc = await payload . create ( {
132
156
collection : 'media' ,
133
157
data : image2 ,
134
- filePath : path . resolve ( dirname , 'image-hero1.webp' ) ,
158
+ file : hero1Buffer ,
135
159
req,
136
160
} )
137
161
@@ -360,3 +384,23 @@ export const seed = async ({
360
384
361
385
payload . logger . info ( 'Seeded database successfully!' )
362
386
}
387
+
388
+ async function fetchFileByURL ( url : string ) : Promise < File > {
389
+ const res = await fetch ( url , {
390
+ credentials : 'include' ,
391
+ method : 'GET' ,
392
+ } )
393
+
394
+ if ( ! res . ok ) {
395
+ throw new Error ( `Failed to fetch file from ${ url } , status: ${ res . status } ` )
396
+ }
397
+
398
+ const data = await res . arrayBuffer ( )
399
+
400
+ return {
401
+ name : url . split ( '/' ) . pop ( ) || `file-${ Date . now ( ) } ` ,
402
+ data : Buffer . from ( data ) ,
403
+ mimetype : `image/${ url . split ( '.' ) . pop ( ) } ` ,
404
+ size : data . byteLength ,
405
+ }
406
+ }
0 commit comments