-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadPackages.php
More file actions
150 lines (131 loc) · 4.99 KB
/
UploadPackages.php
File metadata and controls
150 lines (131 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <info@arikaim.com>
* @license http://www.arikaim.com/license
* @package CoreAPI
*/
namespace Arikaim\Core\Api;
use Arikaim\Core\Controllers\ControlPanelApiController;
use Arikaim\Core\Utils\File;
use Arikaim\Core\Utils\ZipFile;
use Arikaim\Core\Utils\Path;
use Arikaim\Core\Controllers\Traits\FileUpload;
/**
* UploadPackage controller
*/
class UploadPackages extends ControlPanelApiController
{
use FileUpload;
/**
* Init controller
*
* @return void
*/
public function init()
{
$this->loadMessages('system:admin.messages');
}
/**
* Get package info
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Psr\Http\Message\ResponseInterface $response
* @param Validator $data
* @return Psr\Http\Message\ResponseInterface
*/
public function packageInfo($request, $response, $data)
{
}
/**
* Confirm Upload package
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Psr\Http\Message\ResponseInterface $response
* @param Validator $data
* @return Psr\Http\Message\ResponseInterface
*/
public function confirmUpload($request, $response, $data)
{
$data->validate(true);
$packageDir = $data->get('package_directory');
$sourcePath = Path::STORAGE_TEMP_PATH . $packageDir;
if (File::exists($sourcePath) == false) {
$this->error('errors.package.temp');
return;
}
$packageInfo = File::readJsonFile($sourcePath . DIRECTORY_SEPARATOR . 'arikaim-package.json');
if ($packageInfo === false) {
$this->error('errors.package.json');
return;
}
$destinatinPath = $this->get('packages')->getPackagePath($packageInfo['package-type']);
$destinatinPath = $destinatinPath . $packageInfo['name'];
$result = File::copy($sourcePath,$destinatinPath);
$this->setResponse($result,function() use($packageDir) {
$this
->message('package.upload')
->field('package',$packageDir);
},'errors.package.upload');
}
/**
* Upload package
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Psr\Http\Message\ResponseInterface $response
* @param Validator $data
* @return mixed
*/
public function upload($request, $response, $data)
{
$data
->validate(true);
$files = $this->uploadFiles($request,Path::STORAGE_TEMP_PATH,false);
$packageDir = null;
// process uploaded files
foreach ($files as $item) {
if (empty($item['error']) == false) continue;
$fileUploaded = $item['name'];
if (File::getExtension($fileUploaded) == 'zip') {
// unzip
$fileName = Path::STORAGE_TEMP_PATH . $fileUploaded;
$packageDir = ZipFile::getItemPath($fileName,0);
ZipFile::extract($fileName,Path::STORAGE_TEMP_PATH);
break;
}
}
if (empty($packageDir) == true) {
$this->error('errors.package.unzip');
return;
}
$result = $this->get('storage')->has('temp/' . $packageDir);
if ($result == false) {
$this->error('errors.package.upload');
return;
}
$packageInfo = File::readJsonFile(Path::STORAGE_TEMP_PATH . $packageDir . DIRECTORY_SEPARATOR . 'arikaim-package.json');
if ($packageInfo === false) {
$this->error('errors.package.json');
return;
}
$packagePath = $this->get('packages')->getPackagePath($packageInfo['package-type']);
$destinationPath = Path::getRelativePath($packagePath) . $packageInfo['name'];
$currentPackage = false;
if (File::exists($packagePath . $packageInfo['name']) == true) {
$packageManager = $this->get('packages')->create($packageInfo['package-type']);
$package = $packageManager->createPackage($packageInfo['name']);
$currentPackage = $package->getProperties()->toArray();
}
$this->setResponse($result,function() use($fileUploaded, $packageInfo, $destinationPath, $currentPackage, $packageDir) {
$this
->message('package.upload')
->field('file_uploaded',$fileUploaded)
->field('package',$packageInfo)
->field('destination',$destinationPath)
->field('package_directory',$packageDir)
->field('current',$currentPackage);
},'errors.package.upload');
}
}