first (part 3: src directory)
Deploy Iddaai Backend / build-and-deploy (push) Successful in 33s

This commit is contained in:
2026-04-16 15:12:27 +03:00
parent 2f0b85a0c7
commit 182f4aae16
125 changed files with 22552 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
import { existsSync, createWriteStream, mkdirSync } from 'fs';
import { dirname } from 'path';
import axios from 'axios';
import { Logger } from '@nestjs/common';
export class ImageUtils {
private static readonly logger = new Logger('ImageUtils');
/**
* Downloads an image from a URL and saves it to a local path.
* Skips download if file already exists.
*/
static async downloadImage(url: string, localPath: string): Promise<boolean> {
try {
// Check if file exists
if (existsSync(localPath)) {
return true;
}
// Ensure directory exists
const dir = dirname(localPath);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
// Download
const response = await axios({
url,
method: 'GET',
responseType: 'stream',
timeout: 5000,
validateStatus: (status) => status === 200, // Only save if 200 OK
});
const writer = createWriteStream(localPath);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', () => resolve(true));
writer.on('error', (err) => {
this.logger.warn(
`Failed to write image to ${localPath}: ${err.message}`,
);
reject(new Error(`Failed to write image to ${localPath}`));
});
});
} catch (error: any) {
// Log warning but don't break the application
// 404s are common for missing logos
if (error.response?.status !== 404) {
this.logger.warn(
`Failed to download image from ${url}: ${error.message}`,
);
}
return false;
}
}
}