|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BEdita, API-first content management framework |
|
4
|
|
|
* Copyright 2021 ChannelWeb Srl, Chialab Srl |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
|
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace App\Controller; |
|
14
|
|
|
|
|
15
|
|
|
use App\Controller\AppController; |
|
16
|
|
|
use Cake\Core\Configure; |
|
17
|
|
|
use Cake\Core\InstanceConfigTrait; |
|
18
|
|
|
use Cake\Http\Client; |
|
19
|
|
|
use Cake\Http\Response; |
|
20
|
|
|
use Cake\Utility\Hash; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Download Controller |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
class DownloadController extends AppController |
|
27
|
|
|
{ |
|
28
|
|
|
use InstanceConfigTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritDoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $_defaultConfig = [ |
|
34
|
|
|
// HTTP client configuration |
|
35
|
|
|
'client' => [], |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Download a single stream |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $uuid Stream UUID |
|
42
|
|
|
* @return \Cake\Http\Response |
|
43
|
|
|
*/ |
|
44
|
|
|
public function download(string $uuid): Response |
|
45
|
|
|
{ |
|
46
|
|
|
$response = $this->apiClient->get(sprintf('/streams/%s', $uuid)); |
|
47
|
|
|
$stream = (array)Hash::get($response, 'data'); |
|
48
|
|
|
$filename = Hash::get($stream, 'attributes.file_name'); |
|
49
|
|
|
$contentType = Hash::get($stream, 'attributes.mime_type'); |
|
50
|
|
|
|
|
51
|
|
|
// output |
|
52
|
|
|
$response = $this->response->withStringBody($this->content($stream)); |
|
53
|
|
|
$response = $response->withType($contentType); |
|
54
|
|
|
|
|
55
|
|
|
return $response->withDownload($filename); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Return file content as string |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $stream Stream data |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function content(array $stream): string |
|
65
|
|
|
{ |
|
66
|
|
|
$url = Hash::get($stream, 'meta.url'); |
|
67
|
|
|
if (!empty($url)) { |
|
68
|
|
|
return (string)file_get_contents($url); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->streamDownload($stream); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Download file content via `/streams/download` |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $stream Stream data |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function streamDownload(array $stream): string |
|
81
|
|
|
{ |
|
82
|
|
|
$headers = [ |
|
83
|
|
|
'Authorization' => sprintf('Bearer %s', Hash::get($this->apiClient->getTokens(), 'jwt')), |
|
84
|
|
|
'X-Api-Key' => Configure::read('API.apiKey'), |
|
85
|
|
|
'Accept' => Hash::get($stream, 'attributes.mime_type'), |
|
86
|
|
|
]; |
|
87
|
|
|
$url = sprintf('%s/streams/download/%s', $this->apiClient->getApiBaseUrl(), $stream['id']); |
|
88
|
|
|
|
|
89
|
|
|
$client = new Client((array)$this->getConfig('client')); |
|
90
|
|
|
$response = $client->get($url, [], compact('headers')); |
|
91
|
|
|
|
|
92
|
|
|
return $response->getStringBody(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|