-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { fetchAListInfo } from '~/server/lib/query' | ||
|
||
export async function POST(request: Request) { | ||
const formData = await request.formData() | ||
|
||
const file = formData.get('file') | ||
const storage = formData.get('storage') | ||
const type = formData.get('type') | ||
const mountPath = formData.get('mountPath') || '' | ||
|
||
if (storage && storage.toString() === 's3') { | ||
return Response.json({ url: '' }) | ||
} else { | ||
const findConfig = await fetchAListInfo() | ||
const alistToken = findConfig.find((item) => item.config_key === 'alist_token')?.config_value || ''; | ||
const alistUrl = findConfig.find((item) => item.config_key === 'alist_url')?.config_value || ''; | ||
|
||
const filePath = encodeURIComponent(`${mountPath.toString() === '/' ? '' : mountPath}/${type}/${file?.name}`) | ||
console.log(filePath) | ||
const data = await fetch(`${alistUrl}/api/fs/put`, { | ||
method: 'PUT', | ||
headers: { | ||
'Authorization': alistToken.toString(), | ||
'File-Path': filePath.toString(), | ||
}, | ||
body: file, | ||
}).then((res) => res.json()) | ||
if (data?.code === 200) { | ||
const res = await fetch(`${alistUrl}/api/fs/get`, { | ||
method: 'POST', | ||
headers: { | ||
'Authorization': alistToken.toString(), | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ path: decodeURIComponent(filePath) }) | ||
}).then((res) => res.json()) | ||
console.log(res) | ||
if (res?.code === 200) { | ||
return Response.json({ code: 200, message: '文件上传成功!', data: res?.data }) | ||
} else { | ||
return Response.json({ code: 500, message: '文件路径获取失败!', data: null }) | ||
} | ||
} | ||
} | ||
return Response.json({ code: 500, message: '文件上传失败!', data: null }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use client' | ||
|
||
import React from 'react' | ||
import { InboxOutlined } from '@ant-design/icons' | ||
import type { UploadProps } from 'antd' | ||
import { Upload } from 'antd' | ||
import { toast } from 'sonner' | ||
|
||
export default function FileUpload() { | ||
|
||
const { Dragger } = Upload; | ||
|
||
async function onRequestUpload(option: any) { | ||
const formData = new FormData() | ||
|
||
formData.append('file', option.file) | ||
formData.append('storage', 'alist') | ||
formData.append('type', 'test') | ||
formData.append('mountPath', '/') | ||
const data = await fetch('/api/file-upload', { | ||
method: 'POST', | ||
body: formData | ||
}).then((res) => res.json()) | ||
console.log(data) | ||
if (data?.code === 200) { | ||
option.onSuccess(option.file) | ||
toast.success('文件上传成功!') | ||
} else { | ||
option.onError(option.file) | ||
toast.error('文件上传失败!') | ||
} | ||
} | ||
|
||
const props: UploadProps = { | ||
listType: "picture", | ||
name: 'file', | ||
multiple: false, | ||
maxCount: 1, | ||
customRequest: (file) => onRequestUpload(file), | ||
onChange: (event) => { | ||
console.log(event) | ||
} | ||
} | ||
|
||
return ( | ||
<Dragger {...props}> | ||
<p className="ant-upload-drag-icon"> | ||
<InboxOutlined /> | ||
</p> | ||
<p className="ant-upload-text">点击上传文件或拖拽文件到这里</p> | ||
<p className="ant-upload-hint"> | ||
Vercel 等平台 Free 订阅限制上传大小 6M。 | ||
</p> | ||
</Dragger> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,7 @@ | |
--background-end-rgb: 0, 0, 0; | ||
} | ||
} | ||
|
||
.ant-upload-list-item-progress { | ||
display: none; | ||
} |