Skip to content

Image upload problem #84

@Noadmin-Computer

Description

@Noadmin-Computer

Hello Im trying to upload many images but if i upload images my texts are not saving, if i don't upload image my texts are saving, its on projectModel, pls help

import { componentLoader } from './componentLoader.js'

// Ensure the assets/img directory exists
const dirname = url.fileURLToPath(new URL('.', import.meta.url))
const imgDir = path.join(dirname, '../assets/img')
if (!fs.existsSync(imgDir)) {
fs.mkdirSync(imgDir, { recursive: true })
}

const localProvider = {
provider: { local: { bucket: imgDir, baseUrl: '/img' } },
}
const uploadFeatureOptions = (sectionName = '', isImageField = false) => {
const baseOptions = {
...localProvider,
componentLoader,
properties: {
file: isImageField ? ${sectionName}.file : 'file',
filePath: isImageField ? ${sectionName}.imagePath : 'imagePath',
key: isImageField ? ${sectionName}.image : 'image',
mimeType: isImageField ? ${sectionName}.mimeType : 'mimeType',
filesToDelete: isImageField
? ${sectionName}.filesToDelete
: 'filesToDelete',
title: isImageField ? null : ${sectionName}.title, // Handle text fields separately
description: isImageField ? null : ${sectionName}.description, // Handle text fields separately
},
validation: { mimeTypes: ['image/png', 'image/jpeg', 'image/jpg'] },
uploadPath: (record, filename) => {
const uploadPath = ${record.id()}/${sectionName || 'global'}/${filename}
console.log(Generated upload path for ${sectionName}: ${uploadPath})
return uploadPath
},
hooks: {
after: async (response, request, context) => {
const { record, uploadOptions } = context

			// Save record only if it's not an image field
			try {
				if (!isImageField && !uploadOptions.properties.filePath) {
					await record.save()
					console.log('Record saved successfully.')
				}
			} catch (error) {
				console.error('Error saving record:', error)
				throw error // Rethrow the error to handle it upstream
			}

			return response
		},
	},
}

return baseOptions

}
const resources = [
{
resource: projectModel,
options: {
properties: {
'main.title': { type: 'string', isVisible: true },
'main.description': { type: 'string', isVisible: true },
'main.image': { type: 'file', isVisible: true }, // Image field for main section
'main.imagePath': { type: 'string', isVisible: false },
'gallery.$.image': { type: 'file', isVisible: true }, // Image field for gallery section
'gallery.$.imagePath': { type: 'string', isVisible: false },
'about.title': { type: 'string', isVisible: true },
'about.description': { type: 'string', isVisible: true },
'about.image': { type: 'file', isVisible: true }, // Image field for about section
'about.imagePath': { type: 'string', isVisible: false },
'whatHave.title': { type: 'string', isVisible: true },
'whatHave.description': { type: 'string', isVisible: true },
'whatHave.image': { type: 'file', isVisible: true }, // Image field for whatHave section
'whatHave.imagePath': { type: 'string', isVisible: false },
'style.title': { type: 'string', isVisible: true },
'style.description': { type: 'string', isVisible: true },
'style.image': { type: 'file', isVisible: true }, // Image field for style section
'style.imagePath': { type: 'string', isVisible: false },
},
},
features: [
uploadFeature(uploadFeatureOptions()), // For general file uploads
uploadFeature(uploadFeatureOptions('main', true)), // For main section images
uploadFeature(uploadFeatureOptions('gallery', true)), // For gallery section images
uploadFeature(uploadFeatureOptions('about', true)), // For about section images
uploadFeature(uploadFeatureOptions('whatHave', true)), // For whatHave section images
uploadFeature(uploadFeatureOptions('style', true)), // For style section images
],
},]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions