コード例 #1
0
 /**
  * @Route("/", methods="POST")
  * @Route("/{id}", methods="POST", requirements={"id"="\d+"})
  * @Request({"file": "array", "id": "int"}, csrf=true)
  */
 public function saveAction($data, $id = 0)
 {
     /** @var File $file */
     if (!$id || !($file = File::where(compact('id'))->related('categories')->first())) {
         if ($id) {
             App::abort(404, __('File not found.'));
         }
         $file = File::create();
     }
     if (!($data['slug'] = App::filter($data['slug'] ?: $data['title'], 'slugify'))) {
         App::abort(400, __('Invalid slug.'));
     }
     $file->save($data);
     $file->saveCategories($data['category_ids']);
     return ['message' => 'success', 'file' => $file];
 }
コード例 #2
0
 /**
  * @Route("/file/edit", name="file/edit")
  * @Access("download: manage downloads")
  * @Request({"id": "int"})
  */
 public function editAction($id = 0)
 {
     try {
         if (!($file = File::where(compact('id'))->related('categories')->first())) {
             if ($id) {
                 App::abort(404, __('Invalid file id.'));
             }
             $file = File::create(['status' => 1, 'slug' => '', 'data' => [], 'tags' => [], 'date' => new \DateTime()]);
             $file->set('markdown', $this->download->config('markdown'));
         }
         return ['$view' => ['title' => $id ? __('Edit download') : __('Add download'), 'name' => 'bixie/download/admin/file.php'], '$data' => ['categories' => Category::findAll(), 'statuses' => File::getStatuses(), 'roles' => array_values(Role::findAll()), 'config' => $this->download->config(), 'file' => $file, 'tags' => File::allTags()], 'file' => $file];
     } catch (\Exception $e) {
         App::message()->error($e->getMessage());
         return App::redirect('@download/download');
     }
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 public function generate(array $parameters = [])
 {
     $id = $parameters['id'];
     if (isset($parameters['category_id'])) {
         unset($parameters['category_id']);
     }
     if (!isset($this->cacheEntries[$id])) {
         if (!($file = File::where(compact('id'))->first())) {
             throw new RouteNotFoundException('File not found.');
         }
         $this->addCache($file);
     }
     $meta = $this->cacheEntries[$id];
     $parameters['slug'] = $meta['slug'];
     unset($parameters['id']);
     return $parameters;
 }
コード例 #4
0
 /**
  * @Route("/{id}", name="id")
  * @Request({"id": "integer", "key": "string", "pkey": "string"})
  * @param integer $id File id
  * @param string $key session key
  * @param string $purchaseKey optional purchase key
  * @return BinaryFileResponse
  */
 public function downloadAction($id, $key, $purchaseKey)
 {
     //todo return proper errors
     if (!($file = File::where(['id = ?', 'status = ?'], [$id, 1])->first())) {
         App::abort(404, __('File not found.'));
     }
     if (!$file->hasAccess(App::user())) {
         App::abort(403, __('Insufficient User Rights.'));
     }
     if (!$this->download->checkDownloadKey($file, $key, $purchaseKey)) {
         App::abort(400, __('Key not valid.'));
     }
     $file->updateDownloadCount();
     // Generate response
     $response = new BinaryFileResponse($file->path);
     $response->headers->set('Content-Disposition', $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, basename($file->path), mb_convert_encoding(basename($file->path), 'ASCII')));
     return $response;
 }
コード例 #5
0
 /**
  * {@inheritdoc}
  */
 public function match(array $parameters = [])
 {
     if (isset($parameters['id'])) {
         return $parameters;
     }
     if (!isset($parameters['slug'])) {
         App::abort(404, 'File not found.');
     }
     if (!isset($parameters['category_id'])) {
         App::abort(404, 'No category provided.');
     }
     $slug = $parameters['slug'];
     $category_id = $parameters['category_id'];
     $id = false;
     foreach ($this->cacheEntries as $entry) {
         if ($entry['slug'] === $slug && $entry['category_id'] === $category_id) {
             $id = $entry['id'];
         }
     }
     if (!$id) {
         /** @var File $file */
         if (!($file = File::where(compact('slug'))->related('categories')->first())) {
             App::abort(404, 'File not found.');
         }
         if (!in_array($category_id, $file->getCategoryIds())) {
             App::abort(400, 'File not in category');
         }
         if (!($category = Category::where(['id' => $category_id])->first())) {
             App::abort(404, 'Category not found.');
         }
         $this->addCache($file, $category->id, $category->slug);
         $category_id = $category->id;
         $id = $file->id;
     }
     $parameters['id'] = $id;
     $parameters['category_id'] = $category_id;
     return $parameters;
 }
コード例 #6
0
 /**
  * @Route("/{id}", name="id")
  * @Request({"id":"int", "category_id":"int"})
  */
 public function fileAction($id = 0, $category_id = 0)
 {
     /** @var File $file */
     if (!($file = File::where(['id = ?', 'status = ?'], [$id, '1'])->where(function ($query) {
         return $query->where('roles IS NULL')->whereInSet('roles', App::user()->roles, false, 'OR');
     })->first())) {
         App::abort(404, __('File not found.'));
     }
     $file->setActiveCategory($category_id);
     App::trigger('bixie.prepare.file', [$file, App::view()]);
     $file->content = App::content()->applyPlugins($file->content, ['file' => $file, 'markdown' => $file->get('markdown')]);
     $previous = File::getPrevious($file);
     $next = File::getNext($file);
     /** @var Category $category */
     if ($category_id && !($category = Category::where(['id = ?', 'status = ?'], [$category_id, '1'])->where(function ($query) {
         return $query->where('roles IS NULL')->whereInSet('roles', App::user()->roles, false, 'OR');
     })->related('files')->first())) {
         App::abort(404, __('Category not found.'));
     }
     if ($breadcrumbs = App::module('bixie/breadcrumbs')) {
         if ($category_id) {
             $cat = $category;
             $crumbs = [['title' => $category->title, 'url' => $category->getUrl()]];
             while ($parent_id = $cat->parent_id) {
                 if ($cat = $cat->find($parent_id, true)) {
                     $crumbs[] = ['title' => $cat->title, 'url' => $cat->getUrl()];
                 }
             }
             foreach (array_reverse($crumbs) as $data) {
                 $breadcrumbs->addUrl($data);
             }
         }
         //add file
         $breadcrumbs->addUrl(['title' => $file->title, 'url' => $file->getUrl()]);
     }
     return ['$view' => ['title' => __($file->title), 'name' => 'bixie/download/file.php'], 'download' => $this->download, 'config' => $this->download->config(), 'previous' => $previous, 'next' => $next, 'file' => $file, 'node' => App::node()];
 }
コード例 #7
0
ファイル: downloads.php プロジェクト: Bixie/pagekit-download
<?php

use Bixie\Download\Model\File;
return ['name' => 'bixie/downloads', 'label' => 'Downloads', 'events' => ['view.scripts' => function ($event, $scripts) use($app) {
    $scripts->register('widget-downloads', 'bixie/download:app/bundle/widget-downloads.js', ['~widgets']);
}], 'render' => function ($widget) use($app) {
    $files = File::where(['status = :status'], ['status' => File::STATUS_PUBLISHED])->get();
    $view = $widget->get('view', 'list');
    return $app['view']('bixie/download/widgets/downloads-' . $view . '.php', compact('widget', 'files'));
}];