Add presigned S3 media URLs
فشلت بعض الفحوصات
Deploy To Ghaymah / deploy (push) Has been cancelled

هذا الالتزام موجود في:
boutmoun123
2026-06-25 17:38:08 +03:00
الأصل 6752ee5fbe
التزام fc7c93ed3e
9 ملفات معدلة مع 463 إضافات و198 حذوفات

عرض الملف

@@ -0,0 +1,63 @@
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { from, Observable, switchMap } from 'rxjs';
import { MediaStorageService } from '../media/media-storage.service';
@Injectable()
export class MediaUrlInterceptor implements NestInterceptor {
constructor(private readonly mediaStorageService: MediaStorageService) {}
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
if (context.getType() !== 'http' || !this.mediaStorageService.shouldSignResponseUrls()) {
return next.handle();
}
return next
.handle()
.pipe(switchMap((data) => from(this.resolveValue(data, new WeakMap<object, unknown>()))));
}
private async resolveValue(value: unknown, seen: WeakMap<object, unknown>): Promise<unknown> {
if (typeof value === 'string') {
return this.mediaStorageService.resolveResponseUrl(value);
}
if (
value === null ||
typeof value !== 'object' ||
value instanceof Date ||
Buffer.isBuffer(value)
) {
return value;
}
const existing = seen.get(value);
if (existing) {
return existing;
}
if (Array.isArray(value)) {
const result: unknown[] = [];
seen.set(value, result);
const entries = await Promise.all(value.map((entry) => this.resolveValue(entry, seen)));
result.push(...entries);
return result;
}
const serializable =
typeof (value as { toJSON?: unknown }).toJSON === 'function'
? (value as { toJSON: () => unknown }).toJSON()
: value;
if (serializable !== value) {
return this.resolveValue(serializable, seen);
}
const result: Record<string, unknown> = {};
seen.set(value, result);
await Promise.all(
Object.entries(value).map(async ([key, entryValue]) => {
result[key] = await this.resolveValue(entryValue, seen);
}),
);
return result;
}
}