fix: auto-create company logo directory on upload

With MySQL migration, data/companies/{id}/ directories may not exist.
Now creates directory automatically instead of returning 404.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-10 16:59:49 +02:00
parent c8dce63f6e
commit 0605b646cf

14
api.php
View File

@@ -720,13 +720,13 @@ switch ($action) {
$ext = $extMap[$detectedType] ?? 'png';
$newFilename = 'logo.' . $ext;
$compDir = DATA_DIR . '/companies/' . $companyId;
// Luo kansio tarvittaessa (data on nyt MySQL:ssä, kansio vain logoille)
if (!file_exists($compDir)) {
http_response_code(404);
echo json_encode(['error' => 'Yritystä ei löydy']);
break;
mkdir($compDir, 0755, true);
}
// Poista vanha logo
// Poista vanha logo ja päivitä kantaan
$companies = dbLoadCompanies();
$found = false;
foreach ($companies as $comp) {
if ($comp['id'] === $companyId) {
$oldLogo = $comp['logo_file'] ?? '';
@@ -735,9 +735,15 @@ switch ($action) {
}
$comp['logo_file'] = $newFilename;
dbSaveCompany($comp);
$found = true;
break;
}
}
if (!$found) {
http_response_code(404);
echo json_encode(['error' => 'Yritystä ei löydy']);
break;
}
move_uploaded_file($file['tmp_name'], $compDir . '/' . $newFilename);
echo json_encode([
'success' => true,