-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandleUpload.ts
44 lines (38 loc) · 1.02 KB
/
handleUpload.ts
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
import type { HandleUpload } from '@payloadcms/plugin-cloud-storage/types'
import type { Octokit } from 'octokit'
import path from 'node:path'
import type { GithubAuthor } from './types'
import { getFileSHA } from './utilities'
interface Args {
author?: GithubAuthor
branch: string
committer?: GithubAuthor
getStorageClient: () => Octokit
owner: string
prefix?: string
repo: string
}
export const getHandleUpload = ({
author,
branch,
committer,
getStorageClient,
owner,
prefix = '',
repo,
}: Args): HandleUpload => {
return async ({ data, file }) => {
const sha = await getFileSHA(getStorageClient, owner, repo, branch, prefix, file.filename)
await getStorageClient().rest.repos.createOrUpdateFileContents({
author,
branch,
committer,
content: file.buffer.toString('base64'),
message: data.commitMessage || `:arrow_up_small: Upload "${file.filename}"`,
owner,
path: path.posix.join(data.prefix || prefix, file.filename),
repo,
sha,
})
}
}