Example #1
0
 public function actionView($id, $nocache = true)
 {
     $filename = Yii::$app->fileStorage->get($id, $nocache);
     Yii::$app->response->format = Response::FORMAT_RAW;
     Yii::$app->response->headers->add('Content-type', FileHelper::getMimeType($filename));
     return file_get_contents($filename);
 }
Example #2
0
 /**
  * Gets the path of the file with $id
  *
  * Method downloads the requested file from the API and saves it to the local machine.
  * Method respects authentication and access rules.
  *
  * @param integer|File $file the ID of the file, or the [[File]] model.
  * When model is passed, no additional query will be performed.
  * @param bool $overrideCache whether the cache must be invalidated
  * @return string full path to the file. File is located under the [[directory]]
  * @throws Exception when fails to save file locally
  * @throws ForbiddenHttpException when file is not available to client due to policies
  */
 public function get($file, $overrideCache = false)
 {
     if (!$file instanceof $this->fileModelClass) {
         $file = $this->getFileModel($file, $overrideCache);
     }
     $path = FileHelper::getPrefixedPath($this->directory, static::buildHash($file->md5));
     if (!is_file($path) || $overrideCache) {
         $content = $file::perform('Get', ['id' => $file->id]);
         if (!FileHelper::createDirectory(dirname($path))) {
             throw new \yii\base\Exception("Failed to create directory");
         }
         if (!file_put_contents($path, $content)) {
             throw new \yii\base\Exception("Failed to create local file");
         }
     }
     $cache = $this->getCache();
     $key = $this->buildCacheKey($file->id);
     $cache->set($key, $file, 0);
     return $path;
 }
Example #3
0
 private function getContentType($id)
 {
     $path = $this->fileStorage->get($id);
     return FileHelper::getMimeType($path);
 }