Nextcloud ok save doc
This commit is contained in:
@@ -219,6 +219,100 @@ class StorageRepositoryController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function importToLocal(Request $request, StorageRepository $storageRepository): JsonResponse
|
||||
{
|
||||
$this->authorizeWrite('documenti');
|
||||
|
||||
$validated = $request->validate([
|
||||
'path' => 'required|string',
|
||||
'basename' => 'required|string|max:255',
|
||||
'cartella_id' => 'nullable|integer|exists:documenti_cartelle,id',
|
||||
'tipologia' => 'nullable|string|in:avatar,galleria,documento,statuto,altro',
|
||||
'visibilita' => 'nullable|string|in:pubblico,individuo,gruppo,evento,mailing',
|
||||
'visibilita_target_id' => 'nullable|integer',
|
||||
]);
|
||||
|
||||
$path = $validated['path'];
|
||||
$basename = $validated['basename'];
|
||||
|
||||
$filesystem = $this->repoService->buildFilesystem($storageRepository);
|
||||
if (!$filesystem) {
|
||||
return response()->json(['success' => false, 'message' => 'Impossibile connettersi al repository remoto.'], 500);
|
||||
}
|
||||
|
||||
$normalizedPath = $this->repoService->normalizePath($path);
|
||||
|
||||
try {
|
||||
$stream = $filesystem->readStream($normalizedPath);
|
||||
} catch (\Exception $e) {
|
||||
Log::error("StorageRepository importToLocal: readStream failed for repo #{$storageRepository->id} path '{$path}': " . $e->getMessage());
|
||||
return response()->json(['success' => false, 'message' => 'File non trovato nel repository remoto o impossibile da leggere.'], 404);
|
||||
}
|
||||
|
||||
if (!$stream) {
|
||||
return response()->json(['success' => false, 'message' => 'Impossibile leggere il file remoto.'], 500);
|
||||
}
|
||||
|
||||
$disk = \App\Models\AppSetting::getDocumentiStorageDisk();
|
||||
$storagePath = \App\Models\AppSetting::getDocumentiStoragePath();
|
||||
|
||||
$extension = strtolower(pathinfo($basename, PATHINFO_EXTENSION));
|
||||
$nomeSenzaEstensione = pathinfo($basename, PATHINFO_FILENAME);
|
||||
$uniqueName = preg_replace('/[^a-zA-Z0-9_\-\x{80}-\x{FF}]/u', '_', $nomeSenzaEstensione) . '_' . time() . ($extension ? '.' . $extension : '');
|
||||
$filePath = $storagePath . '/' . date('Y/m/d') . '/' . $uniqueName;
|
||||
|
||||
$stored = \Illuminate\Support\Facades\Storage::disk($disk)->writeStream($filePath, $stream);
|
||||
fclose($stream);
|
||||
|
||||
if (!$stored) {
|
||||
return response()->json(['success' => false, 'message' => 'Errore durante il salvataggio locale del file.'], 500);
|
||||
}
|
||||
|
||||
$mimeType = null;
|
||||
try { $mimeType = $filesystem->mimeType($path); } catch (\Exception) {}
|
||||
$fileSize = null;
|
||||
try { $fileSize = $filesystem->fileSize($path); } catch (\Exception) {}
|
||||
|
||||
$visibilita = $validated['visibilita'] ?? 'pubblico';
|
||||
$targetType = null;
|
||||
$targetId = null;
|
||||
if ($visibilita === 'individuo' && !empty($validated['visibilita_target_id'])) {
|
||||
$targetType = \App\Models\Individuo::class;
|
||||
$targetId = (int) $validated['visibilita_target_id'];
|
||||
} elseif ($visibilita === 'gruppo' && !empty($validated['visibilita_target_id'])) {
|
||||
$targetType = \App\Models\Gruppo::class;
|
||||
$targetId = (int) $validated['visibilita_target_id'];
|
||||
} elseif ($visibilita === 'evento' && !empty($validated['visibilita_target_id'])) {
|
||||
$targetType = \App\Models\Evento::class;
|
||||
$targetId = (int) $validated['visibilita_target_id'];
|
||||
} elseif ($visibilita === 'mailing' && !empty($validated['visibilita_target_id'])) {
|
||||
$targetType = \App\Models\MailingList::class;
|
||||
$targetId = (int) $validated['visibilita_target_id'];
|
||||
}
|
||||
|
||||
$documento = \App\Models\Documento::create([
|
||||
'nome_file' => $nomeSenzaEstensione,
|
||||
'file_path' => $filePath,
|
||||
'storage_disk' => $disk,
|
||||
'tipo' => 'upload',
|
||||
'tipologia' => $validated['tipologia'] ?? 'documento',
|
||||
'visibilita' => $visibilita,
|
||||
'visibilita_target_id' => $targetId,
|
||||
'visibilita_target_type' => $targetType,
|
||||
'mime_type' => $mimeType,
|
||||
'dimensione' => $fileSize,
|
||||
'user_id' => auth()->id(),
|
||||
'cartella_id' => $validated['cartella_id'] ?? null,
|
||||
'repository_id' => null,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'File importato come documento locale: ' . $documento->nome_file,
|
||||
'documento_id' => $documento->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function oauthRedirect(Request $request): RedirectResponse
|
||||
{
|
||||
$this->authorizeWrite('settings');
|
||||
|
||||
Reference in New Issue
Block a user