class UploaderServlet in N1ED - Visual editor as CKEditor plugin with Bootstrap support 8.2
Uploader servlet (ported from Java). Stores available action processors, routes request to one of them and returns JSON formed by request processor.
Hierarchy
- class \Drupal\n1ed\Flmngr\FileUploaderServer\servlet\UploaderServlet
Expanded class hierarchy of UploaderServlet
1 file declares its use of UploaderServlet
- FileUploaderServer.php in src/
Flmngr/ FileUploaderServer/ FileUploaderServer.php
File
- src/
Flmngr/ FileUploaderServer/ servlet/ UploaderServlet.php, line 20
Namespace
Drupal\n1ed\Flmngr\FileUploaderServer\servletView source
class UploaderServlet {
protected $actions;
protected $json;
protected $uploader;
protected $config;
/**
* Creates an instance.
*/
public function init($config) {
$this->actions = new Actions();
$this->json = new JsonCodec($this->actions);
$this->config = new ServletConfig($config);
$this->uploader = new Uploader($this->config, $this->actions);
}
/**
* Gets file info.
*/
public function getFileInfo($vector) {
$result = [];
foreach ($vector as $key1 => $value1) {
foreach ($value1 as $key2 => $value2) {
$result[$key2][$key1] = $value2;
}
}
return $result;
}
/**
* Gets a request.
*/
protected function getReq(RequestStack $request_stack, $files) {
$req = NULL;
try {
$data = $request_stack
->getCurrentRequest()->request
->get('data') ?? json_encode([
'action' => 'upload',
]);
$req = $this->json
->fromJson($data);
if ($this->config
->isTestAllowed()) {
if (array_key_exists("testServerConfig", $req)) {
$this->config
->setTestConfig($req->testServerConfig);
}
if (array_key_exists("testClearAllFiles", $req)) {
$this
->clearAllFiles();
}
}
} catch (Exception $e) {
error_log($e);
return NULL;
}
if (array_key_exists('file', $files) || array_key_exists('upload', $files)) {
$req->file = $files['file'] ?? $files['upload'];
$req->fileName = $req->file['name'];
$req->fileSize = $req->file['size'];
}
return $req;
}
/**
* Clears all files.
*/
protected function clearAllFiles() {
UtilsPHP::cleanDirectory($this->config
->getTmpDir());
UtilsPHP::cleanDirectory($this->config
->getBaseDir());
}
/**
* Adds headers to response.
*/
protected function addHeaders() {
if ($this->config
->getCrossDomainUrl() != NULL && strlen($this->config
->getCrossDomainUrl()) > 0) {
header("Access-Control-Allow-Origin: " . $this->config
->getCrossDomainUrl());
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: accept, content-type");
header("Access-Control-Max-Age: 1728000");
}
}
/**
* Processes OPTIONS HTTP request.
*/
public function doOptions() {
$this
->addHeaders();
}
/**
* Processes POST HTTP request.
*/
public function doPost(RequestStack $request_stack, $files) {
$this
->addHeaders();
$resp = null;
$strResp = null;
try {
$req = null;
try {
$req = $this
->getReq($request_stack, $files);
} catch (Exception $e) {
error_log($e);
}
if ($req === null) {
$req = new ReqError(Message::createMessage(Message::MALFORMED_REQUEST));
}
$resp = $this->uploader
->run($req);
if ($resp === null) {
throw new Exception("Null response as result");
}
$strResp = $this->json
->toJson($resp);
} catch (Exception $e) {
error_log($e);
$resp = new RespFail(Message::createMessage(Message::INTERNAL_ERROR));
$strResp = $this->json
->toJson($resp);
}
try {
http_response_code(200);
header('Content-Type: application/json; charset=UTF-8');
print $strResp;
die;
} catch (Exception $e) {
error_log($e);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
UploaderServlet:: |
protected | property | ||
UploaderServlet:: |
protected | property | ||
UploaderServlet:: |
protected | property | ||
UploaderServlet:: |
protected | property | ||
UploaderServlet:: |
protected | function | Adds headers to response. | |
UploaderServlet:: |
protected | function | Clears all files. | |
UploaderServlet:: |
public | function | Processes OPTIONS HTTP request. | |
UploaderServlet:: |
public | function | Processes POST HTTP request. | |
UploaderServlet:: |
public | function | Gets file info. | |
UploaderServlet:: |
protected | function | Gets a request. | |
UploaderServlet:: |
public | function | Creates an instance. |