Skip to content

Commit

Permalink
Fix permission issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusklocke committed Dec 3, 2023
1 parent f1d8e17 commit ee225a2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 32 deletions.
1 change: 0 additions & 1 deletion docker-compose.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ services:

php:
image: mklocke/liga-manager-api:latest
user: 1000:1000
volumes:
- .:/var/www/api
- logos:/mnt/logos
Expand Down
5 changes: 4 additions & 1 deletion docker/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ RUN chmod +x /usr/local/bin/install-phar.sh \
COPY docker/php/docker.conf /usr/local/etc/php-fpm.d/docker.conf
COPY docker/php/php.ini /usr/local/etc/php/php.ini

# Add entrypoint
COPY docker/php/entrypoint.sh /usr/local/bin/docker-php-entrypoint
RUN chmod +x /usr/local/bin/docker-php-entrypoint

# Prepare application source directory
ENV APP_HOME="/var/www/api"
RUN mkdir ${APP_HOME}
Expand All @@ -53,7 +57,6 @@ COPY bin bin/

# Configure ownership and permissions
RUN chown -R www-data:www-data ${APP_HOME} && chmod +x bin/*
USER www-data
ENV PATH="${PATH}:${APP_HOME}/bin:${APP_HOME}/vendor/bin"

CMD lima migrations:migrate -n && php-fpm
13 changes: 13 additions & 0 deletions docker/php/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
set -e

# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
set -- php-fpm "$@"
fi

if [ -e /mnt/logos ]; then
chown www-data:www-data /mnt/logos
fi

exec "$@"
33 changes: 3 additions & 30 deletions src/Infrastructure/API/Logos/UploadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function __construct(LoggerInterface $logger)
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$file = $this->getUploadedFile($request);

$this->checkForUploadError($file);
$fileId = $this->generateFileId();

$fileSize = (int)$file->getSize();
if ($fileSize === 0) {
Expand All @@ -41,6 +41,8 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
throw new InvalidInputException("Invalid media type. Expected: image/webp. Got: $mediaType");
}

$fileId = $this->generateFileId();

$file->moveTo($this->buildStoragePath($fileId));

return $response->withHeader('Location', $this->buildPublicPath($fileId));
Expand All @@ -61,35 +63,6 @@ private function generateFileId(): string
return Uuid::create();
}

private function getMaxFileSize(): int
{
$value = ini_get('upload_max_filesize');
$value = trim($value);

if (is_numeric($value)) {
return (int)$value;
}

$unit = substr($value, -1);
$value = substr($value, 0, -1);

switch (strtolower($unit)) {
case 'g':
$value *= 2**30;
break;
case 'm':
$value *= 2**20;
break;
case 'k':
$value *= 2**10;
break;
default:
throw new InternalException('Invalid unit suffix in "upload_max_filesize"');
}

return (int)$value;
}

private function getUploadedFile(ServerRequestInterface $request): UploadedFileInterface
{
$files = $request->getUploadedFiles();
Expand Down

0 comments on commit ee225a2

Please sign in to comment.