Skip to content

Commit

Permalink
指定したfileIdのファイルを取得してUint8Arrayで返す関数を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
kamecha committed Jul 13, 2024
1 parent ed398bb commit 96a84c5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions denops/traqvim/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { OAuth } from "./oauth.ts";
import {
channelMessageOptions,
channelTimeline,
downloadFile,
homeChannelId,
homeChannelPath,
sendMessage,
Expand All @@ -11,6 +12,7 @@ import {
Denops,
ensureArray,
ensureNumber,
ensureObject,
ensureString,
fn,
helper,
Expand Down Expand Up @@ -38,6 +40,14 @@ export async function main(denops: Denops) {
// oauthの仮オブジェクト
let oauth: OAuth;
denops.dispatcher = {
async getFile(fileId: unknown): Promise<unknown> {
ensureString(fileId);
const file: Uint8Array = await downloadFile(fileId);
// denops-sixel-view.vimのsixel_viewがUint8Arrayを受け取れる必要がある
const sixel = denops.dispatch("sixel_view", "img2sixel", file);
ensureObject<{ data: string; height: number; width: number }>(sixel);
return sixel;
},
setupOAuth(): Promise<unknown> {
helper.echo(denops, "setup...");
// OAuthの設定を行う
Expand Down
17 changes: 17 additions & 0 deletions denops/traqvim/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,20 @@ export const removePin = async (
console.error(e);
}
};

export const downloadFile = async (
fileId: string,
): Promise<Uint8Array> => {
try {
const fileRes = await api.fetchWithToken(
"GET",
"/files/" + fileId,
{},
);
const blob = await fileRes.blob();
return new Uint8Array(await blob.arrayBuffer());
} catch (e) {
console.error(e);
}
return new Uint8Array();
};

0 comments on commit 96a84c5

Please sign in to comment.