Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[misskey-js] /drive/files/create へのリクエストが正常にできない? #14144

Closed
1 task
tai-cha opened this issue Jul 6, 2024 · 2 comments · Fixed by #14147
Closed
1 task

[misskey-js] /drive/files/create へのリクエストが正常にできない? #14144

tai-cha opened this issue Jul 6, 2024 · 2 comments · Fixed by #14147
Labels

Comments

@tai-cha
Copy link
Contributor

tai-cha commented Jul 6, 2024

💡 Summary

予想: requestがmultipart/form-dataに対応してない?

自分のやり方が間違っている可能性がある?のでミニマムで再現しようとしたコードサンプルもつけておきます

🥰 Expected Behavior

misskey-jsを用いてドライブにファイルアップロードができる

🤬 Actual Behavior

misskey-jsを用いてドライブにファイルアップロードができない

📝 Steps to Reproduce

最小で再現しようとしたCode Sample (nodejs@20)

Sample
```ts
import { readFileSync } from 'fs';
import * as Misskey from 'misskey-js'

export function fileToBlob(filePath: string): Blob {
  // ファイルをバイナリデータとして読み込む
  const fileBuffer = readFileSync(filePath);

  // バイナリデータをUint8Arrayに変換
  const uint8Array = new Uint8Array(fileBuffer);

  // Blobオブジェクトを作成
  const blob = new Blob([uint8Array]);

  return blob;
}

export async function blobTobinaryText(blob: Blob): Promise<string> {
  return new Promise((resolve, reject) => {
    try {
      // ファイルをバイナリデータとして読み込む
      const fileBuffer = readFileSync(filePath);

      // バッファをバイナリ文字列に変換
      const binaryString = fileBuffer.toString('binary');
      resolve(binaryString);
    } catch (error) {
      reject(error);
    }
  });
}

export async function fileToBinaryText(filePath: string): Promise<string> {
  const blob = fileToBlob(filePath);
  return blobTobinaryText(blob);
}

const filePath = '/home/taichan/heat_storoke_alert_bot/captures/today.png';
const binary = await fileToBinaryText(filePath);

const apiClient = new Misskey.api.APIClient({ origin: 'https://mi.taichan.site', credential: 'It6ipr8s17tFvq6k791YOgyw0kioTB7u' })

// uploaded1 will be TypeError
// const uploaded1 = apiClient.request('drive/files/create', { file: fileToBlob(filePath) } as Misskey.Endpoints['drive/files/create']['req']) //TypeError
const uploaded2 = apiClient.request('drive/files/create', { file: binary } as Misskey.Endpoints['drive/files/create']['req'])

//console.log(uploaded1)
console.log(uploaded2)

// Result: 
// SyntaxError: Unexpected end of JSON input
//    at JSON.parse (<anonymous>)

code environment

  • nodejs 20.5.0
  • misskey-js 2024.5.0
  • typescript 5.5.3

💻 Frontend Environment

* Model and OS of the device(s):
* Browser:
* Server URL:
* Misskey:

🛰 Backend Environment (for server admin)

* Installation Method or Hosting Service:
* Misskey:
* Node:
* PostgreSQL:
* Redis:
* OS and Architecture:

Do you want to address this bug yourself?

  • Yes, I will patch the bug myself and send a pull request
@tai-cha tai-cha added the ⚠️bug? This might be a bug label Jul 6, 2024
@kakkokari-gtyih
Copy link
Contributor

予想: requestがmultipart/form-dataに対応してない?

正解

@tai-cha
Copy link
Contributor Author

tai-cha commented Jul 6, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants