This repository has been archived on 2025-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
szurubooru/src/Api/Jobs/GetPostContentJob.php

46 lines
1.1 KiB
PHP
Raw Normal View History

<?php
class GetPostContentJob extends AbstractJob
{
2014-05-04 16:27:15 +02:00
protected $post;
2014-05-04 16:27:15 +02:00
public function prepare()
{
$this->post = PostModel::getByName($this->getArgument(self::POST_NAME));
2014-05-04 16:27:15 +02:00
}
2014-05-04 16:27:15 +02:00
public function execute()
{
$post = $this->post;
$config = getConfig();
$path = $config->main->filesPath . DS . $post->getName();
$path = TextHelper::absolutePath($path);
if (!file_exists($path))
throw new SimpleNotFoundException('Post file does not exist');
if (!is_readable($path))
throw new SimpleException('Post file is not readable');
$fileName = sprintf('%s_%s_%s.%s',
$config->main->title,
$post->getId(),
join(',', array_map(function($tag) { return $tag->getName(); }, $post->getTags())),
TextHelper::resolveMimeType($post->getMimeType()) ?: 'dat');
$fileName = preg_replace('/[[:^print:]]/', '', $fileName);
return new ApiFileOutput($path, $fileName);
}
public function requiresPrivilege()
{
2014-05-04 16:27:15 +02:00
$post = $this->post;
$privileges = [];
if ($post->isHidden())
2014-05-04 16:27:15 +02:00
$privileges []= new Privilege(Privilege::ViewPost, 'hidden');
2014-05-04 19:06:40 +02:00
$privileges []= new Privilege(Privilege::ViewPost, $post->getSafety()->toString());
2014-05-04 16:27:15 +02:00
return $privileges;
}
}