Completed
Push — stable9 ( ab68a4...eef0b3 )
by Olivier
10s
created

DownloadService::downloadFile()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 5
nop 2
crap 3
1
<?php
2
/**
3
 * ownCloud - galleryplus
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Olivier Paroz <[email protected]>
9
 *
10
 * @copyright Olivier Paroz 2016
11
 */
12
13
namespace OCA\GalleryPlus\Service;
14
15
use OCP\Files\File;
16
17
18
/**
19
 * Prepares the file to download
20
 *
21
 * @package OCA\GalleryPlus\Service
22
 */
23
class DownloadService extends Service {
24
25
	use Base64Encode;
26
27
	/**
28
	 * Downloads the requested file
29
	 *
30
	 * @param File $file
31
	 * @param bool $base64Encode
32
	 *
33
	 * @return array|false
34
	 * @throws NotFoundServiceException
35
	 */
36 7
	public function downloadFile($file, $base64Encode = false) {
37
		try {
38 7
			$this->logger->debug(
39 7
				"[DownloadService] File to Download: {name}", ['name' => $file->getName()]
40
			);
41
			$download = [
42 7
				'preview'  => $file->getContent(),
43 6
				'mimetype' => $file->getMimeType()
44
			];
45
46 6
			if ($base64Encode) {
47 1
				$download['preview'] = $this->encode($download['preview']);
48
			}
49
50 6
			return $download;
51 1
		} catch (\Exception $exception) {
52 1
			throw new NotFoundServiceException('There was a problem accessing the file');
53
		}
54
55
	}
56
57
}
58