Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit 2862865

Browse files
refactor(server): proxy upload provider methods
1 parent 05d1359 commit 2862865

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

server/api/admin/upload/post.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { responses } from '../../../responses'
22
import perms from '../../../util/perms'
3-
import { get as getUploadProvider } from '../../../uploads'
3+
import { upload } from '../../../uploads'
44
import toBuffer from 'data-uri-to-buffer'
55

66
const itemSchema = {
@@ -35,7 +35,6 @@ export default {
3535
},
3636
bodyLimit: 2 ** 30, // 1 GiB
3737
handler: async ({ req }) => {
38-
const uploadProvider = getUploadProvider()
3938
let convertedFiles
4039
try {
4140
convertedFiles = req.body.files.map(({ name, data }) => {
@@ -51,7 +50,7 @@ export default {
5150
try {
5251
const files = await Promise.all(
5352
convertedFiles.map(async ({ name, data }) => {
54-
const url = await uploadProvider.upload(data, name)
53+
const url = await upload(data, name)
5554

5655
return {
5756
name,

server/api/admin/upload/query.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { responses } from '../../../responses'
22
import perms from '../../../util/perms'
3-
import { get as getUploadProvider } from '../../../uploads'
3+
import { getUrl } from '../../../uploads'
44

55
export default {
66
method: 'POST',
@@ -31,11 +31,9 @@ export default {
3131
}
3232
},
3333
handler: async ({ req }) => {
34-
const uploadProvider = getUploadProvider()
35-
3634
const data = await Promise.all(
3735
req.body.uploads.map(async ({ sha256, name }) => {
38-
const url = await uploadProvider.getUrl(sha256, name)
36+
const url = await getUrl(sha256, name)
3937

4038
return {
4139
sha256,

server/uploads/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import config from '../config/server'
22
import path from 'path'
3-
import { Provider, ProviderConstructor } from './types'
3+
import { Provider, ProviderConstructor } from './provider'
44
import { FastifyInstance } from 'fastify'
55

66
let provider: Provider | null = null
@@ -15,10 +15,18 @@ export const init = (app: FastifyInstance | null): void => {
1515
provider = new ProviderClass(config.uploadProvider.options ?? {}, app)
1616
}
1717

18-
export const get = (): Provider => {
18+
export const upload = (data: Buffer, name: string): Promise<string> => {
1919
if (provider === null) {
2020
throw new Error('upload provider called before initialization')
2121
}
2222

23-
return provider
23+
return provider.upload(data, name)
24+
}
25+
26+
export const getUrl = (sha256: string, name: string): Promise<string|null> => {
27+
if (provider === null) {
28+
throw new Error('upload provider called before initialization')
29+
}
30+
31+
return provider.getUrl(sha256, name)
2432
}

0 commit comments

Comments
 (0)