The Site Config Service manages global site settings stored as key-value pairs in the database. It provides methods to retrieve configuration and update the site logo with Cloudinary integration.
Overview
This service acts as a bridge between the application and the site configuration repository, handling logo uploads and configuration retrieval.
Dependencies
siteConfigRepository - Database operations for configuration
uploadRepository - Cloudinary image upload handling
Methods
getConfigMap()
Retrieves all site configuration as a key-value map.
import { siteConfigService } from '$lib/server/services/site-config.service.js';
const config = await siteConfigService.getConfigMap();
Map of configuration keys to their values. Common keys include:
logoUrl: URL of the site logo
- Other custom configuration values
Example Response
{
logoUrl: 'https://res.cloudinary.com/.../logo.png',
// ... other config values
}
Implementation Details
Source: src/lib/server/services/site-config.service.js:6-8
async getConfigMap() {
return await siteConfigRepository.getAll();
}
updateLogo()
Updates the site logo by uploading to Cloudinary and saving the URL in configuration.
import { siteConfigService } from '$lib/server/services/site-config.service.js';
// In a form handler
const logoFile = formData.get('logo');
await siteConfigService.updateLogo(logoFile);
The logo file to upload. Must be a valid image file.
Upload Configuration
The logo is uploaded to Cloudinary with these settings:
- Folder:
provesa-web/site
- Public ID:
logo
- Overwrite:
true (replaces existing logo)
Process Flow
- Upload image to Cloudinary
- Receive secure URL from Cloudinary
- Update
logoUrl configuration in database
Implementation Details
Source: src/lib/server/services/site-config.service.js:14-22
async updateLogo(logoFile) {
const result = await uploadRepository.uploadImage(logoFile, {
folder: 'provesa-web/site',
public_id: 'logo',
overwrite: true
});
await siteConfigRepository.upsert('logoUrl', result.secure_url);
}
Usage Examples
Get Site Configuration
import { siteConfigService } from '$lib/server/services/site-config.service.js';
export async function load() {
const config = await siteConfigService.getConfigMap();
return {
logoUrl: config.logoUrl || '/default-logo.png'
};
}
Update Logo in SvelteKit Route
// src/routes/admin/site-config/+page.server.js
import { siteConfigService } from '$lib/server/services/site-config.service.js';
import { fail } from '@sveltejs/kit';
export const actions = {
updateLogo: async ({ request }) => {
const formData = await request.formData();
const logoFile = formData.get('logo');
if (!logoFile || logoFile.size === 0) {
return fail(400, { error: 'No logo file provided' });
}
try {
await siteConfigService.updateLogo(logoFile);
return { success: true };
} catch (error) {
return fail(500, { error: 'Failed to upload logo' });
}
}
};
<script>
export let form;
export let data;
</script>
<form method="POST" action="?/updateLogo" enctype="multipart/form-data">
<div>
<label for="logo">Site Logo</label>
<input
type="file"
id="logo"
name="logo"
accept="image/*"
required
/>
</div>
{#if data.logoUrl}
<img src="{data.logoUrl}" alt="Current logo" class="preview" />
{/if}
<button type="submit">Update Logo</button>
{#if form?.success}
<p class="success">Logo updated successfully!</p>
{/if}
{#if form?.error}
<p class="error">{form.error}</p>
{/if}
</form>
Configuration Keys
Common configuration keys managed by this service:
| Key | Type | Description |
|---|
logoUrl | string | URL of the uploaded site logo |
Additional configuration keys can be added through the repository’s upsert method.
Cloudinary Integration
Logo Storage
Logos are stored in Cloudinary with a consistent structure:
provesa-web/
└── site/
└── logo (with file extension)
The overwrite: true option ensures that uploading a new logo replaces the previous one, preventing orphaned files.
Cloudinary returns URLs in this format:
https://res.cloudinary.com/[cloud-name]/image/upload/v[version]/provesa-web/site/logo.[ext]
Error Handling
The updateLogo() method will throw errors if:
- The file upload to Cloudinary fails
- The database update fails
- Invalid file type is provided
Example error handling:
try {
await siteConfigService.updateLogo(logoFile);
} catch (error) {
if (error.message.includes('Cloudinary')) {
console.error('Upload failed:', error);
} else {
console.error('Database error:', error);
}
}
Best Practices
Validate image files before uploading:
- Check file size (recommended max: 2MB)
- Verify file type (accept: image/png, image/jpeg, image/svg+xml)
- Validate dimensions if needed
Cache configuration values at the application level to reduce database queries.