Skip to content

Commit

Permalink
Merge pull request #594 from justabot/download_updates
Browse files Browse the repository at this point in the history
Download updates
  • Loading branch information
lalalune authored Nov 25, 2024
2 parents 90332b2 + 684984b commit 3fbad4b
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions packages/client-direct/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
import { stringToUuid } from "@ai16z/eliza";
import { settings } from "@ai16z/eliza";
import { createApiRouter } from "./api.ts";
import * as fs from "fs";
import * as path from "path";
const upload = multer({ storage: multer.memoryStorage() });

export const messageHandlerTemplate =
Expand Down Expand Up @@ -295,27 +297,63 @@ export class DirectClient {
}
);
this.app.get(
"/fine-tune/:assetId",
"/fine-tune/:assetId",
async (req: express.Request, res: express.Response) => {
const assetId = req.params.assetId;
const downloadDir = path.join(process.cwd(), 'downloads', assetId);

console.log('Download directory:', downloadDir);

try {
const response = await fetch(`https://api.bageldb.ai/api/v1/asset/${assetId}/download`, {
console.log('Creating directory...');
await fs.promises.mkdir(downloadDir, { recursive: true });

console.log('Fetching file...');
const fileResponse = await fetch(`https://api.bageldb.ai/api/v1/asset/${assetId}/download`, {
headers: {
'X-API-KEY': `${process.env.BAGEL_API_KEY}`
}
});

if (!fileResponse.ok) {
throw new Error(`API responded with status ${fileResponse.status}: ${await fileResponse.text()}`);
}

console.log('Response headers:', fileResponse.headers);

const fileName = fileResponse.headers.get('content-disposition')
?.split('filename=')[1]
?.replace(/"/g, '') || 'default_name.txt';

// Forward the content-type header
res.set('Content-Type', response.headers.get('content-type'));
console.log('Saving as:', fileName);

// Convert ReadableStream to Buffer and send
const arrayBuffer = await response.arrayBuffer();
const arrayBuffer = await fileResponse.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
res.send(buffer);

const filePath = path.join(downloadDir, fileName);
console.log('Full file path:', filePath);

await fs.promises.writeFile(filePath, buffer);

// Verify file was written
const stats = await fs.promises.stat(filePath);
console.log('File written successfully. Size:', stats.size, 'bytes');

res.json({
success: true,
message: 'Single file downloaded successfully',
downloadPath: downloadDir,
fileCount: 1,
fileName: fileName,
fileSize: stats.size
});

} catch (error) {
console.error('Detailed error:', error);
res.status(500).json({
error: 'Failed to forward request to BagelDB',
details: error.message
error: 'Failed to download files from BagelDB',
details: error.message,
stack: error.stack
});
}
}
Expand Down

0 comments on commit 3fbad4b

Please sign in to comment.