Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 40 additions & 29 deletions app/Repositories/Daemon/DaemonFileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Exceptions\Repository\FileNotEditableException;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Http\Client\Response;

class DaemonFileRepository extends DaemonRepository
Expand All @@ -22,21 +23,27 @@ class DaemonFileRepository extends DaemonRepository
*/
public function getContent(string $path, ?int $notLargerThan = null): string
{
$response = $this->getHttpClient()->get("/api/servers/{$this->server->uuid}/files/contents",
['file' => $path]
);
try {
$response = $this->getHttpClient()->get("/api/servers/{$this->server->uuid}/files/contents",
['file' => $path]
);
} catch (RequestException $exception) {
$status = $exception->response->status();

$length = $response->header('Content-Length');
if ($notLargerThan && $length > $notLargerThan) {
throw new FileSizeTooLargeException();
}
if ($status === 400) {
throw new FileNotEditableException();
}

if ($status === 404) {
throw new FileNotFoundException();
}

if ($response->status() === 400) {
throw new FileNotEditableException();
throw $exception;
}

if ($response->status() === 404) {
throw new FileNotFoundException();
$length = $response->header('Content-Length');
if ($notLargerThan && $length > $notLargerThan) {
throw new FileSizeTooLargeException();
Comment thread
rxtted marked this conversation as resolved.
}

return $response;
Expand All @@ -51,16 +58,18 @@ public function getContent(string $path, ?int $notLargerThan = null): string
*/
public function putContent(string $path, string $content): Response
{
$response = $this->getHttpClient()
->withQueryParameters(['file' => $path])
->withBody($content)
->post("/api/servers/{$this->server->uuid}/files/write");
try {
return $this->getHttpClient()
->withQueryParameters(['file' => $path])
->withBody($content)
->post("/api/servers/{$this->server->uuid}/files/write");
} catch (RequestException $exception) {
if ($exception->response->status() === 400) {
throw new FileExistsException();
}

if ($response->status() === 400) {
throw new FileExistsException();
throw $exception;
}

return $response;
}

/**
Expand All @@ -85,18 +94,20 @@ public function getDirectory(string $path): array
*/
public function createDirectory(string $name, string $path): Response
{
$response = $this->getHttpClient()->post("/api/servers/{$this->server->uuid}/files/create-directory",
[
'name' => $name,
'path' => $path,
]
);
try {
return $this->getHttpClient()->post("/api/servers/{$this->server->uuid}/files/create-directory",
[
'name' => $name,
'path' => $path,
]
);
} catch (RequestException $exception) {
if ($exception->response->status() === 400) {
throw new FileExistsException();
}

if ($response->status() === 400) {
throw new FileExistsException();
throw $exception;
}

return $response;
}

/**
Expand Down
3 changes: 0 additions & 3 deletions app/Repositories/Daemon/DaemonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ protected function enforceValidNodeToken(Response|bool $condition): bool
if (is_bool($condition)) {
return $condition;
}
if ($condition->clientError()) {
return false;
}

$header = $condition->header('User-Agent');
if (
Expand Down
Loading