-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageKit.ts
More file actions
93 lines (82 loc) · 2.69 KB
/
Copy pathimageKit.ts
File metadata and controls
93 lines (82 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'use node'
import { internalAction } from '~/convex/_generated/server'
import { v } from 'convex/values'
import ImageKit from 'imagekit'
export const imageKit = new ImageKit({
publicKey: process.env.IMAGEKIT_PUBLIC_KEY as string,
privateKey: process.env.IMAGEKIT_PRIVATE_KEY as string,
urlEndpoint: process.env.IMAGEKIT_URL_ENDPOINT as string
})
export const uploadLogo = internalAction({
args: { logo: v.string(), name: v.string() },
handler: async (_, { name, logo }) => {
const fileName = name.replaceAll(' ', '').toLowerCase()
const defaultLogo = await imageKit.upload({
file: logo,
fileName: `${fileName}.svg`,
useUniqueFileName: true,
folder: '/hack_stack/logos'
})
const darkLogo = await imageKit.upload({
file: logo,
fileName: `${fileName}.dark.svg`,
useUniqueFileName: true,
folder: '/hack_stack/logos'
})
return {
logo: defaultLogo.url,
darkLogo: darkLogo.url,
logoIds: [defaultLogo.fileId, darkLogo.fileId]
}
}
})
export const deleteSuggestionLogo = internalAction({
args: { logoIds: v.array(v.string()) },
handler: async (_, { logoIds }) => {
return imageKit.bulkDeleteFiles(logoIds)
}
})
export const approveSuggestionLogo = internalAction({
args: { logo: v.string(), darkLogo: v.string(), newName: v.string() },
handler: async (_, { logo, darkLogo, newName }) => {
const logoName = logo.split('/').pop() as string
const darkLogoName = darkLogo.split('/').pop() as string
try {
await imageKit.renameFile({
filePath: `/hack_stack/logos/${logoName}`,
newFileName: `${newName}.svg`,
purgeCache: true
})
await imageKit.renameFile({
filePath: `/hack_stack/logos/${darkLogoName}`,
newFileName: `${newName}.dark.svg`,
purgeCache: true
})
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
} catch (error: any) {
if (error?.reason === 'FILE_ALREADY_EXISTS') {
// move the original files
await imageKit.moveFile({
sourceFilePath: `/hack_stack/logos/${logoName}`,
destinationPath: '/hack_stack/trash/'
})
await imageKit.moveFile({
sourceFilePath: `/hack_stack/logos/${darkLogoName}`,
destinationPath: '/hack_stack/trash/'
})
}
}
}
})
export const uploadCoverImage = internalAction({
args: { coverImage: v.string(), name: v.string() },
handler: async (_, { name, coverImage }) => {
const coverImageResponse = await imageKit.upload({
file: coverImage,
fileName: `${name}.jpg`,
useUniqueFileName: true,
folder: '/hack_stack/backgrounds'
})
return coverImageResponse.name
}
})