Fix media upload logic: generate DB document with magic SEO formatting
This commit is contained in:
parent
ad31570067
commit
df1df8a681
@ -97,6 +97,10 @@ export async function uploadMediaDocumentServer(formData: FormData) {
|
|||||||
const databases = new Databases(client);
|
const databases = new Databases(client);
|
||||||
const files = formData.getAll('files') as File[];
|
const files = formData.getAll('files') as File[];
|
||||||
|
|
||||||
|
const tituloBase = formData.get('titulo')?.toString() || '';
|
||||||
|
const altTextBase = formData.get('alt_text')?.toString() || '';
|
||||||
|
const descSeo = formData.get('descripcion_seo')?.toString() || '';
|
||||||
|
|
||||||
if (!files || files.length === 0) {
|
if (!files || files.length === 0) {
|
||||||
throw new Error('No se encontraron archivos para subir');
|
throw new Error('No se encontraron archivos para subir');
|
||||||
}
|
}
|
||||||
@ -107,11 +111,20 @@ export async function uploadMediaDocumentServer(formData: FormData) {
|
|||||||
const inputFile = InputFile.fromBuffer(buffer, file.name);
|
const inputFile = InputFile.fromBuffer(buffer, file.name);
|
||||||
const uploadedFile = await storage.createFile(APPWRITE_ALMEIRA_CONFIG.mediaBucketId, ID.unique(), inputFile);
|
const uploadedFile = await storage.createFile(APPWRITE_ALMEIRA_CONFIG.mediaBucketId, ID.unique(), inputFile);
|
||||||
|
|
||||||
|
// Generador mágico de SEO (quitar extensión, guiones, y números finales)
|
||||||
|
const rawName = file.name.split('.').slice(0, -1).join('.'); // Sin extensión
|
||||||
|
const cleanName = rawName
|
||||||
|
.replace(/[-_]/g, ' ') // Cambiar guiones o underscores por espacios
|
||||||
|
.replace(/\d+$/, '') // Eliminar números al final (foto1 -> foto)
|
||||||
|
.trim();
|
||||||
|
const smartTitle = cleanName.charAt(0).toUpperCase() + cleanName.slice(1);
|
||||||
|
|
||||||
const doc = await databases.createDocument(DATABASE_ID, 'media', ID.unique(), {
|
const doc = await databases.createDocument(DATABASE_ID, 'media', ID.unique(), {
|
||||||
file_id: uploadedFile.$id,
|
file_id: uploadedFile.$id,
|
||||||
bucket_id: APPWRITE_ALMEIRA_CONFIG.mediaBucketId,
|
bucket_id: APPWRITE_ALMEIRA_CONFIG.mediaBucketId,
|
||||||
titulo: file.name,
|
titulo: tituloBase || smartTitle || file.name,
|
||||||
alt_text: file.name.split('.')[0],
|
alt_text: altTextBase || smartTitle || rawName,
|
||||||
|
descripcion_seo: descSeo,
|
||||||
tipo: file.type.startsWith('image/') ? 'imagen' : 'video'
|
tipo: file.type.startsWith('image/') ? 'imagen' : 'video'
|
||||||
});
|
});
|
||||||
results.push(doc);
|
results.push(doc);
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { uploadMediaServer } from '@/app/actions/appwrite';
|
import { uploadMediaDocumentServer } from '@/app/actions/appwrite';
|
||||||
import { UploadCloud, Layers, Plus } from 'lucide-react';
|
import { UploadCloud, Layers, Plus } from 'lucide-react';
|
||||||
|
|
||||||
export default function NewMediaPage() {
|
export default function NewMediaPage() {
|
||||||
@ -38,7 +38,7 @@ export default function NewMediaPage() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Native Server Action Upload - Bypass browser authentication errors
|
// Native Server Action Upload - Bypass browser authentication errors
|
||||||
const res = await uploadMediaServer(formData);
|
const res = await uploadMediaDocumentServer(formData);
|
||||||
|
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
router.push('/dashboard/media');
|
router.push('/dashboard/media');
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user