Skip to content

Commit

Permalink
入力された文字列が動画のURLでなかったときの処理を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
cffnpwr committed Jan 23, 2024
1 parent d168aff commit dd6a9e2
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 4 deletions.
32 changes: 32 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions src/function.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
assertEquals,
assertObjectMatch,
} from "https://deno.land/std@0.212.0/assert/mod.ts";
import { getInfo } from "./function.ts";

Deno.test("動画の情報を取得できる", async () => {
const result = await getInfo(
"https://submarin.online/files/3e327bd6-8a90-49d0-8aed-1864c20e5209",
);

assertEquals(result.result, "ok");
assertObjectMatch(result, {
result: "ok",
fileInfo: {
name: "mov,mp4,m4a,3gp,3g2,mj2",
bitRate: 56565,
duration: 3750000,
url: "/3e327bd6-8a90-49d0-8aed-1864c20e5209",
nbStreams: 1,
flags: 2097152,
nbChapters: 0,
streams: [
{
id: 1,
startTime: 0,
duration: 57600,
codecType: 0,
codecName: "avc1",
format: "yuv420p",
bitRate: 48947,
profile: "High",
level: 13,
width: 468,
height: 82,
channels: 0,
sampleRate: 0,
frameSize: 0,
tags: [],
},
],
},
});
});

Deno.test("URLでない文字列を渡すとエラーになる", async () => {
const result = await getInfo("hoge");

assertEquals(result.result, "err");
assertObjectMatch(result, { result: "err", error: "Invalid input: not URL" });
});

Deno.test("動画でないファイルを渡すとエラーになる", async () => {
const result = await getInfo(
"https://submarin.online/files",
);

assertEquals(result.result, "err");
assertObjectMatch(result, {
result: "err",
error: "Invalid input: not video",
});
});
21 changes: 17 additions & 4 deletions src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,30 @@ export const getInfo = async (
{ result: "ok"; fileInfo: FileInfo } | { result: "err"; error: string }
> => {
if (!(path instanceof URL)) {
path = new URL(path);
try {
path = new URL(path);
} catch {
return { result: "err", error: "Invalid input: not URL" };
}
}
const filename = `/${path.pathname.split("/").slice(-1)[0]}`;
if (!filename) {
return { result: "err", error: "Invalid input: not video" };
}
const file = wasi.fs.open(filename, {
read: true,
write: true,
create: true,
});
await fetch(path).then((res) => res.arrayBuffer()).then((buf) =>
file.write(new Uint8Array(buf))
);
const res = await fetch(path);
if (!res.ok || !res.headers.get("Content-Type")?.includes("video")) {
res.body?.cancel();

return { result: "err", error: "Invalid input: not video" };
}

const buf = await res.arrayBuffer();
file.write(new Uint8Array(buf))
file.seek(0);

const { ptr, len } = utf8Encode(filename);
Expand Down

0 comments on commit dd6a9e2

Please sign in to comment.