87 lines
2.4 KiB
TypeScript
87 lines
2.4 KiB
TypeScript
import 'dotenv/config';
|
|
|
|
import { upload } from '../upload';
|
|
import { createLog } from '../log';
|
|
import { File } from '../file';
|
|
|
|
import chokidar from 'chokidar';
|
|
import type { Logger } from 'winston';
|
|
import type { UploadResult, S3Config } from '../upload';
|
|
import type { FileInfo } from '../file';
|
|
|
|
const EXPIRATION : number = 3600; //1 hour
|
|
|
|
const log : Logger = createLog('files');
|
|
|
|
async function process (filePath : string) {
|
|
const config : S3Config = {
|
|
region: 'us-east-1',
|
|
bucketName: 'your-bucket-name',
|
|
expirationSeconds: EXPIRATION
|
|
};
|
|
|
|
const fileInfo : FileInfo = await File.info(filePath);
|
|
|
|
if (!fileInfo.success) {
|
|
log.error(`Error processing file info`, fileInfo.error);
|
|
return;
|
|
}
|
|
|
|
log.info(`Started upload: ${filePath}`);
|
|
const result : UploadResult = await upload('test', filePath, config);
|
|
|
|
if (result.success) {
|
|
log.info('File ${filePath} uploaded successfully!');
|
|
log.info('Private URL:', result.url);
|
|
} else {
|
|
log.error('Upload failed:', result.error);
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
chokidar.watch('./watch', { ignored: /(^|[/\\])\../ }).on('all', (event : any, path : string) => {
|
|
log.info(`File ${path} changed with event type ${event}`);
|
|
});
|
|
}
|
|
|
|
main().catch(log.error);
|
|
|
|
/*
|
|
const minioResult = await uploadFileToS3('/path/to/your/file.pdf', {
|
|
region: 'us-east-1', // Region can be any string for MinIO
|
|
endpoint: 'https://minio.your-domain.com',
|
|
bucketName: 'your-minio-bucket',
|
|
credentials: {
|
|
accessKeyId: 'your-minio-access-key',
|
|
secretAccessKey: 'your-minio-secret-key'
|
|
},
|
|
forcePathStyle: true, // Important for most S3-compatible services
|
|
expirationSeconds: 3600
|
|
});
|
|
|
|
// Example 3: DigitalOcean Spaces
|
|
const spacesResult = await uploadFileToS3('/path/to/your/file.pdf', {
|
|
region: 'nyc3', // DigitalOcean datacenter region
|
|
endpoint: 'https://nyc3.digitaloceanspaces.com',
|
|
bucketName: 'your-space-name',
|
|
credentials: {
|
|
accessKeyId: 'your-spaces-key',
|
|
secretAccessKey: 'your-spaces-secret'
|
|
},
|
|
forcePathStyle: true,
|
|
expirationSeconds: 7200 // 2 hours
|
|
});
|
|
|
|
// Example 4: Wasabi
|
|
const wasabiResult = await uploadFileToS3('/path/to/your/file.pdf', {
|
|
region: 'us-east-1',
|
|
endpoint: 'https://s3.wasabisys.com',
|
|
bucketName: 'your-wasabi-bucket',
|
|
credentials: {
|
|
accessKeyId: 'your-wasabi-access-key',
|
|
secretAccessKey: 'your-wasabi-secret-key'
|
|
},
|
|
forcePathStyle: true,
|
|
expirationSeconds: 86400 // 24 hours
|
|
});
|
|
*/ |