mirror of
https://github.com/aljazceru/nostr-data-vending-machine.git
synced 2025-12-17 06:14:19 +01:00
28 lines
803 B
TypeScript
28 lines
803 B
TypeScript
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;
|
|
} |