Skip to content

Commit

Permalink
patch scripts & add enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayIterator committed Oct 20, 2023
1 parent 5b32067 commit 5ba97c6
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 270 deletions.
230 changes: 115 additions & 115 deletions src/Lang/default.pot

Large diffs are not rendered by default.

232 changes: 116 additions & 116 deletions src/Lang/id.po

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions src/Responder/HtmlResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,18 @@ public function serve(int $code, mixed $data = null, ?ResponseInterface $respons
}

$body->write($this->format($code, $data));
return $this->appendContentType(
$response->withStatus($code)->withBody($body)
);
}

public function appendContentType(ResponseInterface $response) : ResponseInterface
{
$contentType = $this->getContentType();
$charset = $this->getCharset();
if ($charset) {
$contentType .= sprintf('; charset=%s', $charset);
}

return $response
->withStatus($code)
->withHeader('Content-Type', $contentType)
->withBody($body);
return $response->withHeader('Content-Type', $contentType);
}
}
2 changes: 2 additions & 0 deletions src/Responder/Interfaces/ResponderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public function getContentType() : string;
public function getCharset() : ?string;

public function serve(int $code, mixed $data = null, ?ResponseInterface $response = null) : ResponseInterface;

public function appendContentType(ResponseInterface $response) : ResponseInterface;
}
12 changes: 8 additions & 4 deletions src/Responder/JsonResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,19 @@ public function serve(
$body = $this->getStreamFactory()->createStream();
}
$body->write($this->encode($this->format($code, $data, $forceDebug)));
return $this->appendContentType(
$response->withStatus($code)->withBody($body)
);
}

public function appendContentType(ResponseInterface $response) : ResponseInterface
{
$contentType = $this->getContentType();
$charset = $this->getCharset();
if ($charset) {
$contentType .= sprintf('; charset=%s', $charset);
}
return $response
->withStatus($code)
->withHeader('Content-Type', $contentType)
->withBody($body);
return $response->withHeader('Content-Type', $contentType);
}

public function serveJsonMetadata(
Expand Down
47 changes: 27 additions & 20 deletions src/Routing/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use ArrayAccess\TrayDigita\Http\Code;
use ArrayAccess\TrayDigita\Module\Interfaces\ModuleInterface;
use ArrayAccess\TrayDigita\Module\Modules;
use ArrayAccess\TrayDigita\Responder\Interfaces\FileResponderInterface;
use ArrayAccess\TrayDigita\Routing\Interfaces\ControllerInterface;
use ArrayAccess\TrayDigita\Routing\Interfaces\RouteInterface;
use ArrayAccess\TrayDigita\Routing\Interfaces\RouterInterface;
Expand All @@ -29,8 +30,8 @@
use ReflectionMethod;
use Stringable;
use Throwable;
use function is_array;
use function is_int;
use function is_iterable;
use function is_object;
use function is_string;
use function json_decode;
Expand Down Expand Up @@ -262,12 +263,14 @@ public function dispatch(
) {
$statusCode = $this->statusCode;
}
if ($response instanceof FileResponderInterface) {
// stop here
$response->send($request);
}

if (!$response instanceof ResponseInterface) {
$this->response ??= $this->getResponseFactory()->createResponse($statusCode);
if (is_array($response)
|| $response instanceof JsonSerializable
) {
if (is_iterable($response) || $response instanceof JsonSerializable) {
$this->response = $this
->getJsonResponder()
->serve(
Expand All @@ -283,11 +286,8 @@ public function dispatch(
flags: JSON_THROW_ON_ERROR
);
$this->response = $this
->response
->withHeader(
'Content-Type',
$this->getJsonResponder()->getContentType()
);
->getJsonResponder()
->appendContentType($this->response);
} catch (Throwable) {
}
}
Expand All @@ -299,26 +299,29 @@ public function dispatch(
|| is_object($response) && method_exists($response, '__tostring')
) {
$response = (string) $response;
$useJson = false;
if ($this->asJSON) {
try {
json_decode(
$response,
flags: JSON_THROW_ON_ERROR
);
$this->response = $this
->response
->withHeader(
'Content-Type',
$this->getJsonResponder()->getContentType()
);
$useJson = true;
$body = $this->response->getBody()->isWritable()
? $this->response->getBody()
: $this->getStreamFactory()->createStream();
$body->write($response);
$this->response = $this->getJsonResponder()->appendContentType(
$this->response->withBody($body)
);
} catch (Throwable) {
}
}
$body = $this->response->getBody()->isWritable()
? $this->response->getBody()
: $this->getStreamFactory()->createStream();
$body->write($response);
$this->response = $this->response->withBody($body);
if (!$useJson) {
return $this->response = $this
->getHtmlResponder()
->serve($statusCode, $response, $this->response);
}
} elseif ($this->asJSON) {
$this->response = $this
->getJsonResponder()
Expand All @@ -327,6 +330,10 @@ public function dispatch(
$response,
$this->response
);
} else {
$this->response = $this
->getHtmlResponder()
->serve($statusCode, $response, $this->response);
}
} else {
$this->response = $response;
Expand Down
1 change: 1 addition & 0 deletions src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ public function matchRouteParamByRequest(Route $route, ServerRequestInterface $r
if (!str_starts_with($path, $basePath)) {
return null;
}

$basePath = rtrim($basePath, '/');
$path = substr($path, strlen($basePath));
if ($path === '') {
Expand Down
2 changes: 1 addition & 1 deletion src/Uploader/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Chunk implements ManagerAllocatorInterface, ContainerIndicateInterface

private int $maxUploadFileSize;

private bool $allowRevertPosition = true;
private bool $allowRevertPosition = false;

public function __construct(
protected ContainerInterface $container,
Expand Down
30 changes: 22 additions & 8 deletions src/Uploader/ChunkHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use function is_file;
use function is_float;
use function is_int;
use function is_numeric;
use function is_string;
use function json_decode;
use function json_encode;
Expand All @@ -33,6 +34,7 @@
use function sprintf;
use function substr;
use function unlink;
use function var_dump;
use const JSON_UNESCAPED_SLASHES;

class ChunkHandler
Expand Down Expand Up @@ -306,6 +308,10 @@ private function writeResource(string $mode, int $offset) : int
);
}

$time = $_SERVER['REQUEST_TIME_FLOAT']??(
$_SERVER['REQUEST_TIME']??null
);
$time = is_numeric($time) ? (float) $time : microtime(true);
$uploadedStream = $this->processor->uploadedFile->getStream();
if ($uploadedStream->isSeekable()) {
$uploadedStream->rewind();
Expand All @@ -322,8 +328,6 @@ private function writeResource(string $mode, int $offset) : int
) : ($offset + $this->written);
flock($this->cacheResource, LOCK_EX);
$written = null;
$time = $_SERVER['REQUEST_FLOAT_TIME']??null;
$time = is_float($time) ? $time : microtime(true);
if (is_file($this->targetCacheMetaFile)) {
$meta = Consolidation::callbackReduceError(fn () => json_decode(
file_get_contents($this->targetCacheMetaFile),
Expand All @@ -340,22 +344,28 @@ private function writeResource(string $mode, int $offset) : int
if (!$valid) {
Consolidation::callbackReduceError(fn () => unlink($this->targetCacheMetaFile));
} else {
$endTime = microtime(true);
$written = $meta;
$written['count'] += 1;
$written['timing'][] = [
'time' => $time,
'start_time' => $time,
'end_time' => $endTime,
'elapsed_time' => $endTime - $time,
'written' => $this->written,
'size' => $this->size
];
}
} elseif ($isFirst) {
$endTime = microtime(true);
$written = [
'first_time' => $time,
'mimetype' => $this->processor->uploadedFile->getClientMediaType(),
'count' => 1,
'timing' => [
[
'time' => $time,
'start_time' => $time,
'end_time' => $endTime,
'elapsed_time' => $endTime - $time,
'written' => $this->written,
'size' => $this->size
]
Expand Down Expand Up @@ -390,10 +400,13 @@ public function start(?int $offset = null): int
}

$mode = 'wb+';
if ($offset > 0) {
if ($offset > 0 || $offset < 0) {
// offset start from zero
$maxOffset = $this->size > 0 ? ($this->size - 1) : 0;
$allowRevertPosition = $this->processor->chunk->isAllowRevertPosition();
if (!$allowRevertPosition && $offset !== $this->size
|| $allowRevertPosition && $offset > $this->size
if ($offset < 0
|| !$allowRevertPosition && $offset !== $maxOffset
|| $allowRevertPosition && $offset > $maxOffset
) {
// do delete
$this->deletePartial();
Expand All @@ -407,7 +420,8 @@ public function start(?int $offset = null): int
);
}
}

// start offset
$offset = max($offset, 0);
$this->status = self::STATUS_RESUME;
return $this->writeResource($mode, $offset);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Util/Filter/DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static function isHtmlContentType(
return false;
}

return (bool)preg_match(
return (bool) preg_match(
'~^text/x?html(?:[;\s]|$)~i',
trim($contentType)
);
Expand Down

0 comments on commit 5ba97c6

Please sign in to comment.