mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
177 lines
5.2 KiB
PHP
Executable File
177 lines
5.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models\Presupuestos;
|
|
|
|
use Config\Paths;
|
|
|
|
class PresupuestoFicheroModel extends \App\Models\BaseModel
|
|
{
|
|
protected $table = "presupuesto_ficheros";
|
|
|
|
|
|
/**
|
|
* Whether primary key uses auto increment.
|
|
*
|
|
* @var bool
|
|
*/
|
|
protected $useAutoIncrement = true;
|
|
|
|
|
|
protected $allowedFields = ["presupuesto_id", "nombre", "file_path", "upload_by", "upload_at"];
|
|
protected $returnType = "App\Entities\Presupuestos\PresupuestoFicheroEntity";
|
|
|
|
protected $useTimestamps = false;
|
|
protected $useSoftDeletes = false;
|
|
|
|
public static $labelField = "nombre";
|
|
|
|
/**
|
|
* Devuelve la ruta relativa del archivo dentro de WRITEPATH.
|
|
*
|
|
* @param int $presupuesto_id
|
|
* @param string $filename
|
|
* @return string
|
|
*/
|
|
public function getRelativePath(int $presupuesto_id, string $filename): string
|
|
{
|
|
return config(Paths::class)->presupuestosPath . '/' . $presupuesto_id . '/' . $filename;
|
|
}
|
|
|
|
/**
|
|
* Devuelve la ruta absoluta en el sistema de archivos del servidor.
|
|
*
|
|
* @param int $presupuesto_id
|
|
* @param string $filename
|
|
* @return string
|
|
*/
|
|
public function getAbsolutePath(int $presupuesto_id, string $filename): string
|
|
{
|
|
return WRITEPATH . $this->getRelativePath($presupuesto_id, $filename);
|
|
}
|
|
|
|
|
|
public function saveFileInBBDD($presupuesto_id, $filename, $extension, $user_id)
|
|
{
|
|
try {
|
|
|
|
$new_filename = $this->generateFileHash($filename) . '.' . $extension;
|
|
$relativePath = $this->getRelativePath($presupuesto_id, $new_filename);
|
|
|
|
$this->db->table($this->table . " t1")
|
|
->set('presupuesto_id', $presupuesto_id)
|
|
->set('nombre', $filename)
|
|
->set('file_path', $relativePath)
|
|
->set('upload_by', $user_id)
|
|
->set('upload_at', date('Y-m-d H:i:s'))
|
|
->insert();
|
|
|
|
return $new_filename;
|
|
} catch (\Exception $e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function deleteFiles($presupuesto_id, $old_files = [])
|
|
{
|
|
|
|
$files = $this->db
|
|
->table($this->table . " t1")
|
|
->where('presupuesto_id', $presupuesto_id)->get()->getResult();
|
|
if ($files) {
|
|
foreach ($files as $file) {
|
|
|
|
// se comprueba que el $file->nombre no sea igual a ninguno de los elementos del array $old_files
|
|
if (!in_array($file->nombre, $old_files)) {
|
|
$fullPath = WRITEPATH . $file->file_path;
|
|
if (file_exists($fullPath)) {
|
|
unlink($fullPath);
|
|
}
|
|
|
|
$this->db
|
|
->table($this->table . " t1")
|
|
->where('presupuesto_id', $presupuesto_id)
|
|
->where('nombre', $file->nombre)
|
|
->delete();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public function copyFiles($presupuesto_id_origen, $presupuesto_id_destino)
|
|
{
|
|
|
|
$files = $this->db
|
|
->table($this->table . " t1")
|
|
->where('presupuesto_id', $presupuesto_id_origen)->get()->getResult();
|
|
if ($files) {
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$hash = $this->generateFileHash($file->nombre);
|
|
|
|
// se copia el fichero a la nueva ubicación
|
|
$originalPath = WRITEPATH . $file->file_path;
|
|
$newPath = 'uploads/presupuestos/' . $hash;
|
|
|
|
if (file_exists($originalPath)) {
|
|
copy($originalPath, WRITEPATH . $newPath);
|
|
}
|
|
|
|
$this->db->table($this->table . " t1")
|
|
->set('presupuesto_id', $presupuesto_id_destino)
|
|
->set('nombre', $file->nombre)
|
|
->set('file_path', $newPath)
|
|
->set('upload_by', auth()->user()->id)
|
|
->set('upload_at', date('Y-m-d H:i:s'))
|
|
->insert();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public function getFiles($presupuesto_id)
|
|
{
|
|
return $this->db
|
|
->table($this->table . " t1")
|
|
->where('presupuesto_id', $presupuesto_id)->get()->getResult();
|
|
}
|
|
|
|
public function deleteMissingFiles(int $presupuesto_id, array $keepNames = [])
|
|
{
|
|
$files = $this->getFiles($presupuesto_id);
|
|
$deletedCount = 0;
|
|
|
|
foreach ($files as $file) {
|
|
if (!in_array($file->nombre, $keepNames)) {
|
|
$fullPath = WRITEPATH . $file->file_path;
|
|
if (file_exists($fullPath)) {
|
|
unlink($fullPath);
|
|
}
|
|
$this->db->table($this->table)
|
|
->where('presupuesto_id', $presupuesto_id)
|
|
->where('nombre', $file->nombre)
|
|
->delete();
|
|
|
|
$deletedCount++;
|
|
}
|
|
}
|
|
|
|
return $deletedCount;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Función para convertir el nombre y extensión de un fichero en un hash único
|
|
* usando cifrado simétrico.
|
|
*
|
|
* @param string $filename Nombre del fichero con extensión
|
|
* @return string Hash encriptado del fichero
|
|
*/
|
|
private function generateFileHash($filename)
|
|
{
|
|
return hash('sha256', $filename);
|
|
}
|
|
}
|