const express = require('express'); const axios = require('axios'); const cors = require('cors'); const https = require('https'); // httpsモジュールをインポート const app = express(); const port = 7860; // CORSを有効にする app.use(cors()); // httpsAgentを使って証明書検証を無効にする const agent = new https.Agent({ rejectUnauthorized: false // SSL証明書の検証を無効にする }); // リクエストを受け取るエンドポイント app.get('/', async (req, res) => { try { const fileId = req.query.file_id; // Google Drive のファイルIDのみ受け取る if (!fileId) { return res.status(400).send('file_id is required'); } const cleanFileId = fileId.split('?')[0]; // URLを正しく組み立てる const driveUrl = `https://drive.google.com/uc?export=download&id=${cleanFileId}`; // 実際のAPIリクエストを送信 (バイナリ対応) const response = await axios.get(driveUrl, { httpsAgent: agent, responseType: 'arraybuffer' // ← バイナリを壊さない }); // コンテンツタイプをそのまま流す res.set('Content-Type', response.headers['content-type'] || 'application/octet-stream'); res.status(response.status).send(response.data); } catch (error) { console.error(error); res.status(500).send('Something went wrong'); } }); // サーバーを起動 app.listen(port, () => { console.log(`CORS proxy server running at http://localhost:${port}`); });