feat(saves): Публикация картинок из файлов и по внешним ссылкам
This commit is contained in:
@ -173,4 +173,108 @@ export const savesController = new Elysia({ prefix: '/saves' })
|
||||
description: 'Redirects to a presigned URL for downloading the file',
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
.post(
|
||||
'/external',
|
||||
async ({ body, user, set }) => {
|
||||
if (!user) {
|
||||
set.status = 401;
|
||||
return { error: 'Unauthorized' };
|
||||
}
|
||||
|
||||
try {
|
||||
const save = await savesService.createFromUrl(user.id, body);
|
||||
|
||||
return {
|
||||
id: save.id,
|
||||
name: save.name,
|
||||
type: save.type,
|
||||
url: save.url,
|
||||
visibility: save.visibility,
|
||||
shareUrl: save.visibility === 'link' ? save.shareUrl : undefined,
|
||||
createdAt: save.createdAt.toISOString(),
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error creating save from URL:', error);
|
||||
const errorMessage = error instanceof Error ? error.message : 'Failed to create save from URL';
|
||||
|
||||
if (errorMessage.includes('Unsupported') || errorMessage.includes('not found')) {
|
||||
set.status = 400;
|
||||
} else if (errorMessage.includes('Failed to download') || errorMessage.includes('Failed to scrape')) {
|
||||
set.status = 502;
|
||||
} else {
|
||||
set.status = 500;
|
||||
}
|
||||
|
||||
return { error: errorMessage };
|
||||
}
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
url: t.String({ format: 'uri' }),
|
||||
name: t.Optional(t.String()),
|
||||
description: t.Optional(t.String()),
|
||||
tags: t.Optional(t.Array(t.String())),
|
||||
visibility: t.Optional(t.Union([t.Literal('public'), t.Literal('link')])),
|
||||
}),
|
||||
detail: {
|
||||
tags: ['Saves'],
|
||||
summary: 'Create save from external URL',
|
||||
description: 'Downloads and saves a media file from external source',
|
||||
},
|
||||
auth: true
|
||||
}
|
||||
)
|
||||
|
||||
.post(
|
||||
'/upload',
|
||||
async ({ body, user, set }) => {
|
||||
if (!user) {
|
||||
set.status = 401;
|
||||
return { error: 'Unauthorized' };
|
||||
}
|
||||
|
||||
try {
|
||||
const { file, name, description, tags, visibility } = body;
|
||||
|
||||
const save = await savesService.createFromFile(user.id, file, {
|
||||
name,
|
||||
description,
|
||||
tags,
|
||||
visibility,
|
||||
});
|
||||
|
||||
return {
|
||||
id: save.id,
|
||||
name: save.name,
|
||||
type: save.type,
|
||||
url: save.url,
|
||||
visibility: save.visibility,
|
||||
shareUrl: save.visibility === 'link' ? save.shareUrl : undefined,
|
||||
createdAt: save.createdAt.toISOString(),
|
||||
};
|
||||
} catch (error) {
|
||||
set.status = 500;
|
||||
return {
|
||||
error: error instanceof Error ? error.message : 'Failed to upload file'
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
file: t.File(),
|
||||
name: t.Optional(t.String()),
|
||||
description: t.Optional(t.String()),
|
||||
tags: t.Optional(t.Array(t.String())),
|
||||
visibility: t.Optional(t.Union([t.Literal('public'), t.Literal('link')])),
|
||||
}),
|
||||
type: 'multipart/form-data',
|
||||
detail: {
|
||||
tags: ['Saves'],
|
||||
summary: 'Upload file',
|
||||
description: 'Uploads a media file from device',
|
||||
},
|
||||
auth: true
|
||||
}
|
||||
);
|
||||
|
||||
@ -1,10 +1,103 @@
|
||||
import { eq, and, desc } from 'drizzle-orm';
|
||||
import { db, save, type Save } from '@/db';
|
||||
import { db, save, type Save, type NewSave } from '@/db';
|
||||
import { s3Service } from './s3.service';
|
||||
import { scraperService } from './scraper.service';
|
||||
import { redis } from './redis.service';
|
||||
import { nanoid } from 'nanoid';
|
||||
|
||||
import type {
|
||||
Visibility,
|
||||
CreateSaveFromUrlRequest,
|
||||
} from '@p1ctos4ve/shared-types';
|
||||
|
||||
class SavesService {
|
||||
private readonly CACHE_TTL = 3600;
|
||||
|
||||
async createFromFile(
|
||||
userId: string,
|
||||
file: File,
|
||||
metadata: {
|
||||
name?: string;
|
||||
description?: string;
|
||||
tags?: string[];
|
||||
visibility?: Visibility;
|
||||
}
|
||||
): Promise<Save> {
|
||||
const mimeType = file.type;
|
||||
const fileType = s3Service.getFileType(mimeType);
|
||||
|
||||
if (fileType === 'unknown') {
|
||||
throw new Error('Unsupported file type');
|
||||
}
|
||||
|
||||
const { key, url, size } = await s3Service.uploadFile(file, userId, mimeType);
|
||||
|
||||
const shareUrl =
|
||||
metadata.visibility === 'link' ? this.generateShareUrl() : undefined;
|
||||
|
||||
const newSave: NewSave = {
|
||||
userId,
|
||||
name: metadata.name || file.name || 'Untitled',
|
||||
description: metadata.description || '',
|
||||
type: fileType,
|
||||
tags: metadata.tags || [],
|
||||
visibility: metadata.visibility || 'link',
|
||||
shareUrl,
|
||||
s3Key: key,
|
||||
url,
|
||||
fileSize: size,
|
||||
mimeType,
|
||||
};
|
||||
|
||||
const [savedItem] = await db.insert(save).values(newSave).returning();
|
||||
|
||||
await this.invalidateUserCache(userId);
|
||||
|
||||
return savedItem;
|
||||
}
|
||||
|
||||
async createFromUrl(
|
||||
userId: string,
|
||||
data: CreateSaveFromUrlRequest
|
||||
): Promise<Save> {
|
||||
const scrapedMedia = await scraperService.scrapeUrl(data.url);
|
||||
|
||||
const { buffer, mimeType, size } = await s3Service.downloadFromUrl(
|
||||
scrapedMedia.url
|
||||
);
|
||||
|
||||
const fileType = s3Service.getFileType(mimeType);
|
||||
|
||||
if (fileType === 'unknown') {
|
||||
throw new Error('Unsupported file type');
|
||||
}
|
||||
|
||||
const { key, url } = await s3Service.uploadFile(buffer, userId, mimeType);
|
||||
|
||||
const shareUrl =
|
||||
data.visibility === 'link' ? this.generateShareUrl() : undefined;
|
||||
|
||||
const newSave: NewSave = {
|
||||
userId,
|
||||
name: data.name || scrapedMedia.title || 'Untitled',
|
||||
description: data.description || scrapedMedia.description || '',
|
||||
type: fileType,
|
||||
tags: data.tags || [],
|
||||
visibility: data.visibility || 'link',
|
||||
shareUrl,
|
||||
s3Key: key,
|
||||
url,
|
||||
fileSize: size,
|
||||
mimeType,
|
||||
};
|
||||
|
||||
const [savedItem] = await db.insert(save).values(newSave).returning();
|
||||
|
||||
await this.invalidateUserCache(userId);
|
||||
|
||||
return savedItem;
|
||||
}
|
||||
|
||||
async getById(
|
||||
id: number,
|
||||
requestUserId?: string,
|
||||
@ -87,6 +180,15 @@ class SavesService {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private generateShareUrl(): string {
|
||||
return nanoid(16);
|
||||
}
|
||||
|
||||
private async invalidateUserCache(userId: string): Promise<void> {
|
||||
await redis.del(`user_saves:${userId}`);
|
||||
await redis.del(`public_saves:${userId}`);
|
||||
}
|
||||
}
|
||||
|
||||
export const savesService = new SavesService();
|
||||
|
||||
Reference in New Issue
Block a user