mirror of
https://github.com/aljazceru/nostr-data-vending-machine.git
synced 2025-12-17 06:14:19 +01:00
initial commit
This commit is contained in:
28
src/utils/fetch-file-from-input.ts
Normal file
28
src/utils/fetch-file-from-input.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { NDKTag } from '@nostr-dev-kit/ndk';
|
||||
import axios from 'axios';
|
||||
import fs from 'fs';
|
||||
|
||||
export async function fetchFileFromInput(input: NDKTag): Promise<string> {
|
||||
switch (input[2]) {
|
||||
case 'url': {
|
||||
const url = input[1];
|
||||
const fileExtension = url.split('.').pop();
|
||||
|
||||
// download the file
|
||||
// save it to the local filesystem
|
||||
// return the path to the file
|
||||
const response = await axios.get(url, {
|
||||
responseType: 'stream'
|
||||
});
|
||||
|
||||
const randomName = Math.random().toString(36).substring(7);
|
||||
const path = `./${randomName}.${fileExtension}`;
|
||||
|
||||
await response.data.pipe(fs.createWriteStream(path));
|
||||
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user