Exemplo n.º 1
0
 /**
  *
  * 1. get all files from storage folder
  * 2. check each db entry if not available in file list and set is_deleted = 1
  *
  * @return int count of flagged 'is_deleted' entries
  */
 public static function removeMissingStorageFiles()
 {
     $storageFileList = static::getFindFilesDirectory();
     if (!$storageFileList) {
         return false;
     }
     // check storage files
     $allStorageFileEntries = StorageFile::find()->where(['is_deleted' => 0])->indexBy('id')->all();
     $count = 0;
     foreach ($allStorageFileEntries as $dbfile) {
         $found = false;
         foreach ($storageFileList as $key => $file) {
             if ($dbfile['name_new_compound'] == pathinfo($file, PATHINFO_BASENAME)) {
                 $found = true;
                 break;
             }
         }
         if (!$found) {
             $dbfile->is_deleted = 1;
             $count++;
             $dbfile->update(false);
         }
     }
     return $count;
 }
Exemplo n.º 2
0
 public function actionDownload($id, $hash, $fileName)
 {
     // find file in file query
     $fileData = Yii::$app->storage->findFile(['id' => $id, 'hash_name' => $hash, 'is_deleted' => 0]);
     // proceed when file exists
     if ($fileData) {
         // get file source from storage system
         $fileSourcePath = $fileData->serverSource;
         // verify again against database to add counter
         $model = StorageFile::findOne($fileData->id);
         // proceed when model exists
         if ($model && file_exists($fileSourcePath) && is_readable($fileSourcePath)) {
             // update the model count stats
             $count = $model->passthrough_file_stats + 1;
             $model->passthrough_file_stats = $count;
             $model->update(false);
             // return header informations
             header('Content-Description: File Transfer');
             header('Content-Type: application/octet-stream');
             header('Content-Disposition: attachment; filename="' . basename($fileData->name) . '"');
             header('Content-Transfer-Encoding: binary');
             header('Expires: 0');
             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
             header('Pragma: public');
             header('Content-Length: ' . filesize($fileSourcePath));
             flush();
             readfile($fileSourcePath);
             exit;
         }
     }
     // throw bad request exception.
     throw new BadRequestHttpException("Unable to find requested file.");
 }
Exemplo n.º 3
0
 public static function moveFileToFolder($fileId, $folderId)
 {
     $file = StorageFile::findOne($fileId);
     $file->folder_id = $folderId;
     return $file->update(false);
 }
Exemplo n.º 4
0
 /**
  * @todo its a copy from the old colde, refactor code
  * @param string $fileSource
  * @param string $fileName
  * @param int $folderId
  */
 public function addFile($fileSource, $fileName, $folderId = 0, $isHidden = false)
 {
     if (empty($fileSource) || empty($fileName)) {
         throw new Exception("Unable to create file where file source and/or file name is empty.");
     }
     $fileInfo = FileHelper::getFileInfo($fileName);
     $baseName = Inflector::slug($fileInfo->name, '-');
     $fileHashName = Storage::createFileHash($fileName);
     $fileHash = FileHelper::getFileHash($fileSource);
     $mimeType = FileHelper::getMimeType($fileSource);
     $newName = implode([$baseName . '_' . $fileHashName, $fileInfo->extension], '.');
     $savePath = $this->serverPath . '/' . $newName;
     if (is_uploaded_file($fileSource)) {
         if (!@move_uploaded_file($fileSource, $savePath)) {
             throw new Exception("error while moving uploaded file from {$fileSource} to {$savePath}");
         }
     } else {
         if (!@copy($fileSource, $savePath)) {
             throw new Exception("error while copy file from {$fileSource} to {$savePath}.");
         }
     }
     $model = new StorageFile();
     $model->setAttributes(['name_original' => $fileName, 'name_new' => $baseName, 'name_new_compound' => $newName, 'mime_type' => $mimeType, 'extension' => strtolower($fileInfo->extension), 'folder_id' => (int) $folderId, 'hash_file' => $fileHash, 'hash_name' => $fileHashName, 'is_hidden' => $isHidden ? 1 : 0, 'file_size' => @filesize($savePath)]);
     if ($model->validate()) {
         if ($model->save()) {
             $this->deleteHasCache($this->fileCacheKey);
             $this->_filesArray[$model->id] = $model->toArray();
             return $this->getFile($model->id);
         }
     }
     return false;
 }
Exemplo n.º 5
0
 /**
  *
  * 1. get all files from storage folder
  * 2. check each image/filter db entry if not available in file list and remove them from 'admin_storage_image'
  *
  * @return int count of removed entries
  */
 public static function removeMissingImageFiles()
 {
     $storageFileList = static::getFindFilesDirectory();
     if (!$storageFileList) {
         return false;
     }
     // check storage files
     $allStorageFileEntries = StorageFile::find()->indexBy('id')->all();
     $count = 0;
     // check image filter files
     $imageList = StorageImage::find()->all();
     foreach ($imageList as $image) {
         if (array_key_exists($image['file_id'], $allStorageFileEntries)) {
             $filterImage = $image['filter_id'] . '_' . $allStorageFileEntries[$image['file_id']]['name_new_compound'];
             $found = false;
             foreach ($storageFileList as $key => $file) {
                 if ($filterImage == pathinfo($file, PATHINFO_BASENAME)) {
                     $found = true;
                     break;
                 }
             }
             if (!$found) {
                 $image->delete();
                 $count++;
             }
         }
     }
     return $count;
 }
Exemplo n.º 6
0
 /**
  * delete folder, all subfolders and all included files.
  *
  * 1. search another folders with matching parentIds and call deleteFolder on them
  * 2. get all included files and delete them
  * 3. delete folder
  *
  * @param int $folderId
  * @todo move to storage helpers?
  * @return bool
  */
 public function actionFolderDelete($folderId)
 {
     // find all subfolders
     $matchingChildFolders = StorageFolder::find()->where(['parent_id' => $folderId])->asArray()->all();
     foreach ($matchingChildFolders as $matchingChildFolder) {
         $this->actionFolderDelete($matchingChildFolder['id']);
     }
     // find all attached files and delete them
     $folderFiles = StorageFile::find()->where(['folder_id' => $folderId])->all();
     foreach ($folderFiles as $folderFile) {
         $folderFile->delete();
     }
     // delete folder
     $model = StorageFolder::findOne($folderId);
     if (!$model) {
         return false;
     }
     $model->is_deleted = true;
     return $model->update();
 }
Exemplo n.º 7
0
 public function getFile()
 {
     return $this->hasOne(StorageFile::className(), ['id' => 'file_id']);
 }