-
Notifications
You must be signed in to change notification settings - Fork 1
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
12 changed files
with
252 additions
and
43 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,44 @@ | ||
import t, { i18n_map, I18NMap } from "../server/i18n.ts"; | ||
|
||
export type UploaderProps = { | ||
i18n?: I18NMap; | ||
lang?: string; | ||
}; | ||
|
||
export default function Uploader(props: UploaderProps) { | ||
if (props.i18n) i18n_map.value = props.i18n; | ||
return ( | ||
<form | ||
action="/api/file/upload" | ||
method="post" | ||
encType="multipart/form-data" | ||
> | ||
<input | ||
type="file" | ||
name="file" | ||
required={true} | ||
accept=".jpg,.jpeg,.png" | ||
/> | ||
<br /> | ||
{t("upload.filename")}{" "} | ||
<input | ||
type="text" | ||
name="filename" | ||
placeholder={t("upload.filename")} | ||
/> | ||
<br /> | ||
<input id="is_original" type="checkbox" name="is_original" />{" "} | ||
<label for="is_original">{t("upload.is_original")}</label> | ||
<br /> | ||
<input | ||
id="token" | ||
type="text" | ||
name="token" | ||
placeholder={t("upload.token")} | ||
required={true} | ||
/> | ||
<br /> | ||
<input type="submit" value={t("upload.upload")} /> | ||
</form> | ||
); | ||
} |
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,75 @@ | ||
import { Handlers } from "$fresh/server.ts"; | ||
import type { EhFile } from "../../../db.ts"; | ||
import { get_task_manager } from "../../../server.ts"; | ||
import { return_data, return_error } from "../../../server/utils.ts"; | ||
import { get_string, parse_bool } from "../../../server/parse_form.ts"; | ||
import { fb_get_size } from "../../../thumbnail/ffmpeg_binary.ts"; | ||
import { sure_dir } from "../../../utils.ts"; | ||
import mime from "mime"; | ||
import { extname, join, resolve } from "std/path/mod.ts"; | ||
|
||
export const handler: Handlers = { | ||
async POST(req, _ctx) { | ||
const m = get_task_manager(); | ||
try { | ||
const form = await req.formData(); | ||
const file = form.get("file"); | ||
if (!file) { | ||
return return_error(1, "Missing file."); | ||
} | ||
const mext = typeof file === "string" | ||
? null | ||
: `.${mime.getExtension(file.type)}`; | ||
const filename = (await get_string(form.get("filename"))) || | ||
(typeof file === "string" ? null : file.name); | ||
if (!filename) { | ||
return return_error(2, "Missing filename."); | ||
} | ||
const fext = extname(filename); | ||
const fn = mext == fext | ||
? filename | ||
: `${filename.slice(0, filename.length - fext.length)}${mext}`; | ||
const dir = (await get_string(form.get("dir"))) || | ||
join(m.cfg.base, "uploaded"); | ||
const is_original = await parse_bool( | ||
form.get("is_original"), | ||
false, | ||
); | ||
const token = await get_string(form.get("token")); | ||
if (!token) { | ||
return return_error(3, "Missing token."); | ||
} | ||
const path = join(dir, fn); | ||
await sure_dir(dir); | ||
try { | ||
if (typeof file === "string") { | ||
await Deno.writeTextFile(path, file); | ||
} else { | ||
await Deno.writeFile(path, file.stream()); | ||
} | ||
const size = await fb_get_size(path); | ||
if (!size) { | ||
await Deno.remove(path); | ||
return return_error(4, "Failed to get file size."); | ||
} | ||
const rpath = resolve(path); | ||
const f = { | ||
id: 0, | ||
path: rpath, | ||
width: size.width, | ||
height: size.height, | ||
is_original, | ||
token, | ||
} as EhFile; | ||
const nf = m.db.add_file(f, false); | ||
return return_data(nf); | ||
} catch (e) { | ||
await Deno.remove(path); | ||
throw e; | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
return return_error(500, "Internal Server Error."); | ||
} | ||
}, | ||
}; |
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,31 @@ | ||
import { Handlers, PageProps } from "$fresh/server.ts"; | ||
import GlobalContext from "../components/GlobalContext.tsx"; | ||
import Uploader from "../islands/Upload.tsx"; | ||
import { get_i18nmap, i18n_handle_request } from "../server/i18ns.ts"; | ||
|
||
type Props = { | ||
lang: string; | ||
}; | ||
|
||
export const handler: Handlers<Props> = { | ||
GET(req, ctx) { | ||
const re = i18n_handle_request(req); | ||
if (typeof re === "string") { | ||
return ctx.render({ | ||
lang: re, | ||
}); | ||
} | ||
return re; | ||
}, | ||
}; | ||
|
||
export default function Upload({ data }: PageProps<Props>) { | ||
const i18n = get_i18nmap(data.lang, "upload"); | ||
return ( | ||
<body> | ||
<GlobalContext> | ||
<Uploader i18n={i18n} lang={data.lang} /> | ||
</GlobalContext> | ||
</body> | ||
); | ||
} |
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
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,6 @@ | ||
{ | ||
"upload": "Upload file", | ||
"filename": "Filename", | ||
"is_original": "Marked as original file", | ||
"token": "Token" | ||
} |
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,6 @@ | ||
{ | ||
"upload": "上传文件", | ||
"filename": "文件名", | ||
"is_original": "标记为原始文件", | ||
"token": "Token" | ||
} |