/**
  *  Image manipulation view - Includes a small toolbox for basic
  *  image editing before saving or discarding changes
  */
 public function edit($id, $action = null, array $params = null)
 {
     $image = $this->Images->get($id, ['contain' => 'Uploads']);
     if ($this->request->is(['post', 'put'])) {
         $imgFile = new File($image->upload->full_path);
         // Backup original
         $backupFile = new File(TMP . DS . time() . 'bak');
         $imgFile->copy($backupFile->path);
         if (!$image->hasTmp()) {
             return $this->Flash->error('Sorry, we lost your edit...');
         }
         // Open tmp file
         $tmpFile = new File($image->tmp_path_full);
         // Copy tmp file to permanent location
         if (!$tmpFile->copy($imgFile->path)) {
             return $this->Flash->error('Could not save image');
         }
         // Change recorded image dimentions and size
         $image->upload->size = $tmpFile->size();
         $image->width = $this->Image->width($tmpFile->path);
         $image->height = $this->Image->height($tmpFile->path);
         // Delete tmp file
         $image->deleteTmp();
         if (!$this->Images->save($image)) {
             // Restore file with matching, original size and dimentions
             $backupFile->copy($imgFile->path);
             $backupFile->delete();
             $this->Flash->error('Could not save image.');
             return $this->redirect();
         } else {
             $this->Flash->success('Image edited successfully.');
             return $this->redirect();
         }
     } else {
         if (!$image->hasTmp()) {
             $image->makeTmp();
         }
     }
     $this->set(['image' => $image, 'route' => $this->RedirectUrl->load()]);
 }
示例#2
0
 /**
  * Apply a file range to a file and set the end offset.
  *
  * If an invalid range is requested a 416 Status code will be used
  * in the response.
  *
  * @param File $file The file to set a range on.
  * @param string $httpRange The range to use.
  * @return void
  */
 protected function _fileRange($file, $httpRange)
 {
     list(, $range) = explode('=', $httpRange);
     list($start, $end) = explode('-', $range);
     $fileSize = $file->size();
     $lastByte = $fileSize - 1;
     if ($start === '') {
         $start = $fileSize - $end;
         $end = $lastByte;
     }
     if ($end === '') {
         $end = $lastByte;
     }
     if ($start > $end || $end > $lastByte || $start > $lastByte) {
         $this->statusCode(416);
         $this->header(['Content-Range' => 'bytes 0-' . $lastByte . '/' . $fileSize]);
         return;
     }
     $this->header(['Content-Length' => $end - $start + 1, 'Content-Range' => 'bytes ' . $start . '-' . $end . '/' . $fileSize]);
     $this->statusCode(206);
     $this->_fileRange = [$start, $end];
 }
 /**
  * Returns an array that matches the structure of a regular upload for a local file
  *
  * @param $file The file you want to get an upload array for.
  * @param string Name of the file to use in the upload array.
  * @return array Array that matches the structure of a regular upload
  */
 public static function fileToUploadArray($file, $filename = null)
 {
     $File = new File($file);
     if (empty($fileName)) {
         $filename = basename($file);
     }
     return ['name' => $filename, 'tmp_name' => $file, 'error' => 0, 'type' => $File->mime(), 'size' => $File->size()];
 }
示例#4
0
 /**
  * [_upload description]
  * @param  string  $fileName [description]
  * @param  string  $filePath [description]
  * @param  bool $copy     [description]
  * @return array            [description]
  */
 protected function _upload($fileName, $filePath, $copy = false)
 {
     $data = [];
     if (!file_exists($filePath) || !$this->_isImage($filePath)) {
         return $data;
     }
     $basePath = $this->basePath();
     $pathinfo = pathinfo($fileName);
     $fileName = md5_file($filePath) . '.' . $pathinfo['extension'];
     $fullPath = $basePath . DS . $fileName;
     $folder = new Folder($basePath, true, 0777);
     $transferFn = $copy || !is_uploaded_file($filePath) ? 'copy' : 'move_uploaded_file';
     $existing = file_exists($fullPath);
     if ($existing || call_user_func_array($transferFn, [$filePath, $fullPath])) {
         $file = new File($fullPath);
         $data = ['filename' => $fileName, 'size' => $file->size(), 'mime' => $file->mime()];
     }
     return $data;
 }
示例#5
0
 /**
  * Apply a file range to a file and set the end offset.
  *
  * If an invalid range is requested a 416 Status code will be used
  * in the response.
  *
  * @param \Cake\Filesystem\File $file The file to set a range on.
  * @param string $httpRange The range to use.
  * @return void
  */
 protected function _fileRange($file, $httpRange)
 {
     $fileSize = $file->size();
     $lastByte = $fileSize - 1;
     $start = 0;
     $end = $lastByte;
     preg_match('/^bytes\\s*=\\s*(\\d+)?\\s*-\\s*(\\d+)?$/', $httpRange, $matches);
     if ($matches) {
         $start = $matches[1];
         $end = isset($matches[2]) ? $matches[2] : '';
     }
     if ($start === '') {
         $start = $fileSize - $end;
         $end = $lastByte;
     }
     if ($end === '') {
         $end = $lastByte;
     }
     if ($start > $end || $end > $lastByte || $start > $lastByte) {
         $this->statusCode(416);
         $this->header(['Content-Range' => 'bytes 0-' . $lastByte . '/' . $fileSize]);
         return;
     }
     $this->header(['Content-Length' => $end - $start + 1, 'Content-Range' => 'bytes ' . $start . '-' . $end . '/' . $fileSize]);
     $this->statusCode(206);
     $this->_fileRange = [$start, $end];
 }
 /**
  * Gets information about the file that is being uploaded.
  * - gets the file size
  * - gets the mime type
  * - gets the extension if present
  * - sets the adapter by default to local if not already set
  * - sets the model field to the table name if not already set
  *
  * @param array|\ArrayAccess $upload
  * @param string $field
  * @return void
  */
 public function getFileInfoFromUpload(&$upload, $field = 'file')
 {
     if (!empty($upload[$field]['tmp_name'])) {
         $File = new File($upload[$field]['tmp_name']);
         $upload['filesize'] = $File->size();
         $upload['mime_type'] = $File->mime();
     }
     if (!empty($upload[$field]['name'])) {
         $upload['extension'] = pathinfo($upload[$field]['name'], PATHINFO_EXTENSION);
         $upload['filename'] = $upload[$field]['name'];
     }
 }
示例#7
0
 /**
  * testAppend method
  *
  * @return void
  */
 public function testAppend()
 {
     if (!($tmpFile = $this->_getTmpFile())) {
         return false;
     }
     if (file_exists($tmpFile)) {
         unlink($tmpFile);
     }
     $TmpFile = new File($tmpFile);
     $this->assertFalse(file_exists($tmpFile));
     $fragments = ['CakePHP\'s', ' test suite', ' was here ...'];
     $data = null;
     $size = 0;
     foreach ($fragments as $fragment) {
         $r = $TmpFile->append($fragment);
         $this->assertTrue($r);
         $this->assertTrue(file_exists($tmpFile));
         $data = $data . $fragment;
         $this->assertEquals($data, file_get_contents($tmpFile));
         $newSize = $TmpFile->size();
         $this->assertTrue($newSize > $size);
         $size = $newSize;
         $TmpFile->close();
     }
     $TmpFile->append('');
     $this->assertEquals($data, file_get_contents($tmpFile));
     $TmpFile->close();
 }
示例#8
0
 /**
  * Upload function.
  *
  * @param string $fileName Original name of the tmp file.
  * @param string $filePath Full path to the tmp file.
  * @param bool $copy Whether copy or move the tmp file.
  * @return array
  */
 protected function _upload($fileName, $filePath, $copy = false)
 {
     $data = [];
     if (!file_exists($filePath)) {
         return $data;
     }
     $fileName = $this->generateUniqueFilename($fileName, $filePath);
     $basePath = $this->basePath();
     $fullPath = $basePath . DS . $fileName;
     $folder = new Folder($basePath, true, 0775);
     $transferFn = $copy ? 'copy' : 'move_uploaded_file';
     if (file_exists($fullPath) || call_user_func_array($transferFn, [$filePath, $fullPath])) {
         $file = new File($fullPath);
         if (false !== $file->size()) {
             $data = ['filename' => $fileName, 'size' => $file->size(), 'mime' => $file->mime(), 'created' => Time::now()];
         }
     }
     return $data;
 }
 public function hacerBackup()
 {
     if ($this->request->is('post')) {
         $nombre = date('Y-m-d_His') . '_backup.php';
         $archivo = new File($nombre, true, 0777);
         $tablas = array();
         foreach ($this->request->data as $nombreTabla => $value) {
             if ($value == 1) {
                 $tabla = TableRegistry::get($nombreTabla);
                 $registros = $tabla->find('all');
                 //debug($tabla, true, true);
                 //debug($registros, true, true);
                 $tablas[$nombreTabla] = array();
                 foreach ($registros as $entidad) {
                     $tablas[$nombreTabla][] = $entidad->toArray();
                 }
             }
         }
         //Para exportar los datos al archivo en forma de array
         $bytes = @file_put_contents($archivo->info['basename'], "<?php return " . var_export($tablas, true) . ";");
         debug('Tamaño: ' . $archivo->size(), true, true);
         debug('Tamaño: ' . $bytes, true, true);
         if ($bytes > 9) {
             //Para descargar el archivo creado
             $this->response->file($archivo->path, ['download' => true]);
         } else {
             $this->Flash->error(__('Debe seleccionar al menos una tabla.'));
         }
     }
 }
 /**
  * Rotate image based on degree value
  * 
  * @param Image image - image to manipulate
  * @params array params - manipulation parameters
  *      [degree] - degree to rotate by
  * 
  * @return Image Image entity with new dimensions and size
  */
 public function rotate(Image $image, array $params)
 {
     $path = $image->path();
     $edit = $this->imagine->open($path);
     $edit->rotate($params['degree'])->save($path);
     // Set new size
     $file = new File($path);
     $size = $file->size();
     $image->upload->size = $size;
     $image->width = $edit->getSize()->getWidth();
     $image->height = $edit->getSize()->getHeight();
     return $image;
 }