Merge pull request #519 from nextcloud/bugfix/noid/stream-attachment-download
Fetch file from rootFolder instead of IAppData to use StreamResponse
This commit is contained in:
@@ -29,12 +29,16 @@ use OCA\Deck\StatusException;
|
||||
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
||||
use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
|
||||
use OCP\AppFramework\Http\FileDisplayResponse;
|
||||
use OCP\AppFramework\Http\StreamResponse;
|
||||
use OCP\Files\Cache\IScanner;
|
||||
use OCP\Files\Folder;
|
||||
use OCP\Files\IAppData;
|
||||
use OCP\Files\IRootFolder;
|
||||
use OCP\Files\NotFoundException;
|
||||
use OCP\Files\NotPermittedException;
|
||||
use OCP\Files\SimpleFS\ISimpleFile;
|
||||
use OCP\Files\SimpleFS\ISimpleFolder;
|
||||
use OCP\IConfig;
|
||||
use OCP\IL10N;
|
||||
use OCP\ILogger;
|
||||
use OCP\IRequest;
|
||||
@@ -45,17 +49,23 @@ class FileService implements IAttachmentService {
|
||||
private $appData;
|
||||
private $request;
|
||||
private $logger;
|
||||
private $rootFolder;
|
||||
private $config;
|
||||
|
||||
public function __construct(
|
||||
IL10N $l10n,
|
||||
IAppData $appData,
|
||||
IRequest $request,
|
||||
ILogger $logger
|
||||
ILogger $logger,
|
||||
IRootFolder $rootFolder,
|
||||
IConfig $config
|
||||
) {
|
||||
$this->l10n = $l10n;
|
||||
$this->appData = $appData;
|
||||
$this->request = $request;
|
||||
$this->logger = $logger;
|
||||
$this->rootFolder = $rootFolder;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -174,9 +184,32 @@ class FileService implements IAttachmentService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Workaround until ISimpleFile can be fetched as a resource
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function getFileFromRootFolder(Attachment $attachment) {
|
||||
$folderName = 'file-card-' . (int)$attachment->getCardId();
|
||||
$instanceId = $this->config->getSystemValue('instanceid', null);
|
||||
if ($instanceId === null) {
|
||||
throw new \Exception('no instance id!');
|
||||
}
|
||||
$name = 'appdata_' . $instanceId;
|
||||
$appDataFolder = $this->rootFolder->get($name);
|
||||
$appDataFolder = $appDataFolder->get('deck');
|
||||
$cardFolder = $appDataFolder->get($folderName);
|
||||
return $cardFolder->get($attachment->getData());
|
||||
}
|
||||
|
||||
public function display(Attachment $attachment) {
|
||||
$file = $this->getFileForAttachment($attachment);
|
||||
$response = new FileDisplayResponse($file);
|
||||
$file = $this->getFileFromRootFolder($attachment);
|
||||
if (method_exists($file, 'fopen')) {
|
||||
$response = new StreamResponse($file->fopen('r'));
|
||||
$response->addHeader('Content-Disposition', 'inline; filename="' . rawurldecode($file->getName()) . '"');
|
||||
} else {
|
||||
$response = new FileDisplayResponse($file);
|
||||
}
|
||||
if ($file->getMimeType() === 'application/pdf') {
|
||||
// We need those since otherwise chrome won't show the PDF file with CSP rule object-src 'none'
|
||||
// https://bugs.chromium.org/p/chromium/issues/detail?id=271452
|
||||
|
||||
@@ -38,11 +38,15 @@ use OCA\Deck\StatusException;
|
||||
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
||||
use OCP\AppFramework\Http\FileDisplayResponse;
|
||||
use OCP\AppFramework\Http\Response;
|
||||
use OCP\AppFramework\Http\StreamResponse;
|
||||
use OCP\AppFramework\IAppContainer;
|
||||
use OCP\Files\Folder;
|
||||
use OCP\Files\IAppData;
|
||||
use OCP\Files\IRootFolder;
|
||||
use OCP\Files\SimpleFS\ISimpleFile;
|
||||
use OCP\Files\SimpleFS\ISimpleFolder;
|
||||
use OCP\ICacheFactory;
|
||||
use OCP\IConfig;
|
||||
use OCP\IL10N;
|
||||
use OCP\ILogger;
|
||||
use OCP\IRequest;
|
||||
@@ -61,6 +65,10 @@ class FileServiceTest extends TestCase {
|
||||
private $logger;
|
||||
/** @var FileService */
|
||||
private $fileService;
|
||||
/** @var IRootFolder */
|
||||
private $rootFolder;
|
||||
/** @var IConfig */
|
||||
private $config;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
@@ -68,7 +76,9 @@ class FileServiceTest extends TestCase {
|
||||
$this->appData = $this->createMock(IAppData::class);
|
||||
$this->l10n = $this->createMock(IL10N::class);
|
||||
$this->logger = $this->createMock(ILogger::class);
|
||||
$this->fileService = new FileService($this->l10n, $this->appData, $this->request, $this->logger);
|
||||
$this->rootFolder = $this->createMock(IRootFolder::class);
|
||||
$this->config = $this->createMock(IConfig::class);
|
||||
$this->fileService = new FileService($this->l10n, $this->appData, $this->request, $this->logger, $this->rootFolder, $this->config);
|
||||
}
|
||||
|
||||
public function mockGetFolder($cardId) {
|
||||
@@ -253,33 +263,60 @@ class FileServiceTest extends TestCase {
|
||||
}
|
||||
|
||||
public function testDisplay() {
|
||||
$this->config->expects($this->once())
|
||||
->method('getSystemValue')
|
||||
->willReturn('123');
|
||||
$appDataFolder = $this->createMock(Folder::class);
|
||||
$deckAppDataFolder = $this->createMock(Folder::class);
|
||||
$cardFolder = $this->createMock(Folder::class);
|
||||
$this->rootFolder->expects($this->once())->method('get')->willReturn($appDataFolder);
|
||||
$appDataFolder->expects($this->once())->method('get')->willReturn($deckAppDataFolder);
|
||||
$deckAppDataFolder->expects($this->once())->method('get')->willReturn($cardFolder);
|
||||
$attachment = $this->getAttachment();
|
||||
$file = $this->createMock(ISimpleFile::class);
|
||||
$folder = $this->mockGetFolder('123');
|
||||
$folder->expects($this->once())
|
||||
->method('getFile')
|
||||
->willReturn($file);
|
||||
$file->expects($this->exactly(2))
|
||||
$file = $this->createMock(\OCP\Files\File::class);
|
||||
$cardFolder->expects($this->once())->method('get')->willReturn($file);
|
||||
$file->expects($this->any())
|
||||
->method('getMimeType')
|
||||
->willReturn('image/jpeg');
|
||||
$file->expects($this->any())
|
||||
->method('getName')
|
||||
->willReturn('file1');
|
||||
$file->expects($this->any())
|
||||
->method('fopen')
|
||||
->willReturn('fileresource');
|
||||
$actual = $this->fileService->display($attachment);
|
||||
$expected = new FileDisplayResponse($file);
|
||||
$expected = new StreamResponse('fileresource');
|
||||
$expected->addHeader('Content-Type', 'image/jpeg');
|
||||
$expected->addHeader('Content-Disposition', 'inline; filename="' . rawurldecode($file->getName()) . '"');
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
public function testDisplayPdf() {
|
||||
$this->config->expects($this->once())
|
||||
->method('getSystemValue')
|
||||
->willReturn('123');
|
||||
$appDataFolder = $this->createMock(Folder::class);
|
||||
$deckAppDataFolder = $this->createMock(Folder::class);
|
||||
$cardFolder = $this->createMock(Folder::class);
|
||||
$this->rootFolder->expects($this->once())->method('get')->willReturn($appDataFolder);
|
||||
$appDataFolder->expects($this->once())->method('get')->willReturn($deckAppDataFolder);
|
||||
$deckAppDataFolder->expects($this->once())->method('get')->willReturn($cardFolder);
|
||||
$attachment = $this->getAttachment();
|
||||
$file = $this->createMock(ISimpleFile::class);
|
||||
$folder = $this->mockGetFolder('123');
|
||||
$folder->expects($this->once())
|
||||
->method('getFile')
|
||||
->willReturn($file);
|
||||
$file->expects($this->exactly(2))
|
||||
$file = $this->createMock(\OCP\Files\File::class);
|
||||
$cardFolder->expects($this->once())->method('get')->willReturn($file);
|
||||
$file->expects($this->any())
|
||||
->method('getMimeType')
|
||||
->willReturn('application/pdf');
|
||||
$file->expects($this->any())
|
||||
->method('getName')
|
||||
->willReturn('file1');
|
||||
$file->expects($this->any())
|
||||
->method('fopen')
|
||||
->willReturn('fileresource');
|
||||
$actual = $this->fileService->display($attachment);
|
||||
$expected = new FileDisplayResponse($file);
|
||||
$expected = new StreamResponse('fileresource');
|
||||
$expected->addHeader('Content-Disposition', 'inline; filename="' . rawurldecode($file->getName()) . '"');
|
||||
$expected->addHeader('Content-Type', 'application/pdf');
|
||||
$policy = new ContentSecurityPolicy();
|
||||
$policy->addAllowedObjectDomain('\'self\'');
|
||||
|
||||
Reference in New Issue
Block a user