|
@@ -22,13 +22,13 @@ class MediaController extends AuthController {
|
|
|
|
|
|
$query = $this->queryInicial;
|
|
$query = $this->queryInicial;
|
|
|
|
|
|
- if($id > 0) {
|
|
|
|
|
|
+ if ($id > 0) {
|
|
$query->andWhere(["id" => $id]);
|
|
$query->andWhere(["id" => $id]);
|
|
}
|
|
}
|
|
|
|
|
|
- if($buscar) {
|
|
|
|
|
|
+ if ($buscar) {
|
|
# Ejemplo de buscador
|
|
# Ejemplo de buscador
|
|
-
|
|
|
|
|
|
+
|
|
$query->andWhere([
|
|
$query->andWhere([
|
|
"OR",
|
|
"OR",
|
|
["ilike", "nombre", $buscar]
|
|
["ilike", "nombre", $buscar]
|
|
@@ -40,74 +40,79 @@ class MediaController extends AuthController {
|
|
}
|
|
}
|
|
|
|
|
|
public function actionGuardar() {
|
|
public function actionGuardar() {
|
|
- if (!$this->req->isPost) {
|
|
|
|
- throw new NotFoundHttpException();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- $usuario = \Yii::$app->getUser()->getIdentity();
|
|
|
|
|
|
+ $dominio = Yii::$app->getRequest()->getHostInfo() . "/assets/";
|
|
|
|
+ $archivo = UploadedFile::getInstanceByName('file');
|
|
|
|
+ $tipo = $this->req->getBodyParam("tipo", "");
|
|
|
|
|
|
- $this->res->format = Response::FORMAT_JSON;
|
|
|
|
- $archivo = UploadedFile::getInstanceByName('archivo');
|
|
|
|
if ($archivo === null) {
|
|
if ($archivo === null) {
|
|
return (new Respuesta())
|
|
return (new Respuesta())
|
|
- ->esError()
|
|
|
|
- ->mensaje("No se recibió el archivo");
|
|
|
|
|
|
+ ->mensaje("No se recibió el archivo")
|
|
|
|
+ ->esError();
|
|
}
|
|
}
|
|
|
|
|
|
- $sec = \Yii::$app->getSecurity();
|
|
|
|
- $base = \Yii::getAlias("@app") . "/web/assets/";
|
|
|
|
|
|
+ $sec = Yii::$app->getSecurity();
|
|
|
|
+ $base = Yii::getAlias("@app") . "/web/assets/";
|
|
|
|
|
|
- $ruta = "recurso/";
|
|
|
|
- if (!is_dir($base . $ruta)) {
|
|
|
|
- mkdir($base . $ruta);
|
|
|
|
- }
|
|
|
|
|
|
+ $ruta = "archivos/";
|
|
|
|
+ try {
|
|
|
|
|
|
- $ruta .= date("Y/");
|
|
|
|
- if (!is_dir($base . $ruta)) {
|
|
|
|
- mkdir($base . $ruta);
|
|
|
|
- }
|
|
|
|
|
|
+ if (!is_dir($base . $ruta)) {
|
|
|
|
+ mkdir($base . $ruta);
|
|
|
|
+ }
|
|
|
|
|
|
- $ruta .= date("m/");
|
|
|
|
- if (!is_dir($base . $ruta)) {
|
|
|
|
- mkdir($base . $ruta);
|
|
|
|
- }
|
|
|
|
|
|
+ $ruta .= date("Y/");
|
|
|
|
+ if (!is_dir($base . $ruta)) {
|
|
|
|
+ mkdir($base . $ruta);
|
|
|
|
+ }
|
|
|
|
|
|
- $dominio = \Yii::$app->getRequest()->getHostInfo() . "/assets/";
|
|
|
|
- do {
|
|
|
|
- $nombreArchivo = str_replace("-", "", $ruta . $sec->generateRandomString());
|
|
|
|
- if ($archivo->extension) {
|
|
|
|
- $nombreArchivo .= "." . $archivo->extension;
|
|
|
|
|
|
+ $ruta .= date("m/");
|
|
|
|
+ if (!is_dir($base . $ruta)) {
|
|
|
|
+ mkdir($base . $ruta);
|
|
}
|
|
}
|
|
- } while (is_file($base . $nombreArchivo));
|
|
|
|
- if (!$archivo->saveAs($base . $nombreArchivo)) {
|
|
|
|
|
|
+
|
|
|
|
+ do {
|
|
|
|
+ $nombreArchivo = str_replace("-", "", $ruta . $sec->generateRandomString());
|
|
|
|
+ if ($archivo->extension) {
|
|
|
|
+ $nombreArchivo .= "." . $archivo->extension;
|
|
|
|
+ }
|
|
|
|
+ } while (is_file($base . $nombreArchivo));
|
|
|
|
+
|
|
|
|
+ if (!$archivo->saveAs($base . $nombreArchivo)) {
|
|
|
|
+ //throw new \Exception("Ocurrió un problema al guardar el archivo (29032023)");
|
|
|
|
+ return (new Respuesta())
|
|
|
|
+ ->mensaje("Ocurrió un problema al guardar el archivo (29032023)")
|
|
|
|
+ ->esError();
|
|
|
|
+ }
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
+ //throw new \Exception($e->getMessage() . "error al guardar (12233124)");
|
|
return (new Respuesta())
|
|
return (new Respuesta())
|
|
- ->mensaje("Ocurrió un problema al guardar el archivo");
|
|
|
|
|
|
+ ->mensaje($e->getMessage() . "error al guardar (12233124)")
|
|
|
|
+ ->esError();
|
|
}
|
|
}
|
|
|
|
|
|
- $uuid = Uuid::uuid4();
|
|
|
|
-
|
|
|
|
- $modelo = new Media();
|
|
|
|
-
|
|
|
|
- $modelo->uuid();
|
|
|
|
- $modelo->creado = new Expression('now()');
|
|
|
|
- $modelo->idUsuario = $usuario->id;
|
|
|
|
- $modelo->uuid = $uuid->toString();
|
|
|
|
- $modelo->nombre = $archivo->name;
|
|
|
|
- $modelo->mimetype = $archivo->type;
|
|
|
|
- $modelo->extension = $archivo->extension;
|
|
|
|
- $modelo->ruta = $dominio . $nombreArchivo;
|
|
|
|
-
|
|
|
|
- $modelo->load($this->req->getBodyParams(), '');
|
|
|
|
- if (!$modelo->save()) {
|
|
|
|
- return (new Respuesta($modelo))
|
|
|
|
- ->mensaje("Hubo un problema al guardar Media");
|
|
|
|
|
|
+ $uuid = Uuid::uuid4()->toString();
|
|
|
|
+
|
|
|
|
+ $model = new Media();
|
|
|
|
+ $model->creado = new Expression('now()');
|
|
|
|
+ $model->ruta = $dominio . $nombreArchivo;
|
|
|
|
+ $model->ubicacionFisica = $dominio . $nombreArchivo;
|
|
|
|
+ $model->uuid = $uuid;
|
|
|
|
+ $model->extension = $archivo->extension;
|
|
|
|
+ $model->descripcion = $archivo->name;
|
|
|
|
+ $model->mimeType = $archivo->type;
|
|
|
|
+
|
|
|
|
+ if ($tipo !== "")
|
|
|
|
+ $model->tipo = $tipo;
|
|
|
|
+
|
|
|
|
+ if (!$model->save()) {
|
|
|
|
+ //throw new \Exception("error al guardar (761253)");
|
|
|
|
+ return (new Respuesta($model))
|
|
|
|
+ ->mensaje("error al guardar (761253)")
|
|
|
|
+ ->esError();
|
|
|
|
+ } else {
|
|
|
|
+ return (new Respuesta($model))
|
|
|
|
+ ->mensaje("Archivo Subido correctamente");
|
|
}
|
|
}
|
|
-
|
|
|
|
- $modelo->refresh();
|
|
|
|
-
|
|
|
|
- return (new Respuesta())
|
|
|
|
- ->mensaje("Archivo subido correctamente")
|
|
|
|
- ->detalle($modelo);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public function actionEliminar() {
|
|
public function actionEliminar() {
|
|
@@ -133,4 +138,4 @@ class MediaController extends AuthController {
|
|
}
|
|
}
|
|
return (new Respuesta())->mensaje("Archivo eliminado");
|
|
return (new Respuesta())->mensaje("Archivo eliminado");
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
+}
|