Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleCIBot committed Nov 30, 2024
1 parent 9e901b6 commit 461c694
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/Mime/Mapping/APK.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?php

/*
* This file is part of fof/upload.
*
* Copyright (c) FriendsOfFlarum.
* Copyright (c) Flagrow.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Upload\Mime\Mapping;

class APK extends AbstractMimeMap
Expand Down
10 changes: 10 additions & 0 deletions src/Mime/Mapping/AbstractMimeMap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?php

/*
* This file is part of fof/upload.
*
* Copyright (c) FriendsOfFlarum.
* Copyright (c) Flagrow.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Upload\Mime\Mapping;

abstract class AbstractMimeMap
Expand Down
25 changes: 22 additions & 3 deletions src/Mime/MimeTypeDetector.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?php

/*
* This file is part of fof/upload.
*
* Copyright (c) FriendsOfFlarum.
* Copyright (c) Flagrow.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Upload\Mime;

use Flarum\Foundation\ValidationException;
Expand All @@ -15,30 +25,35 @@ class MimeTypeDetector
* Set the file path for MIME type detection.
*
* @param string $filePath
*
* @return $this
*/
public function forFile(string $filePath): self
{
$this->filePath = $filePath;

return $this;
}

/**
* Set the upload object for fallback extension guessing.
*
* @param UploadedFile $upload
*
* @return $this
*/
public function withUpload(UploadedFile $upload): self
{
$this->upload = $upload;

return $this;
}

/**
* Determine the MIME type of the file.
*
* @throws ValidationException
*
* @return string|null
*/
public function getMimeType(): ?string
Expand Down Expand Up @@ -106,6 +121,7 @@ private function detectUsingMagicBytes(): ?string
* Check if the file is a valid APK by inspecting its contents.
*
* @param string $filePath
*
* @return bool
*/
private function isApk(string $filePath): bool
Expand All @@ -117,22 +133,24 @@ private function isApk(string $filePath): bool
foreach ($requiredFiles as $file) {
if ($zip->locateName($file) === false) {
$zip->close();

return false; // Required APK-specific file not found
}
}
$zip->close();

return true; // All required files found
}

return false; // Not a valid ZIP file
}


/**
* Determine the file extension based on the MIME type or original extension.
*
* @param array $whitelistedExtensions Whitelisted extensions for validation
* @param string|null $originalExtension Original client extension
* @param array $whitelistedExtensions Whitelisted extensions for validation
* @param string|null $originalExtension Original client extension
*
* @return string
*/
public function getFileExtension(array $whitelistedExtensions = [], ?string $originalExtension = null): string
Expand Down Expand Up @@ -166,6 +184,7 @@ public function getFileExtension(array $whitelistedExtensions = [], ?string $ori
* Guess file extension based on MIME type.
*
* @param string|null $mimeType
*
* @return string|null
*/
private function guessExtensionFromMimeType(?string $mimeType): ?string
Expand Down
14 changes: 12 additions & 2 deletions src/Providers/MimeMappingProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?php

/*
* This file is part of fof/upload.
*
* Copyright (c) FriendsOfFlarum.
* Copyright (c) Flagrow.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Upload\Providers;

use Flarum\Foundation\AbstractServiceProvider;
Expand All @@ -19,8 +29,8 @@ public function register()
foreach ($mappings as $mappingClass) {
if (is_subclass_of($mappingClass, AbstractMimeMap::class)) {
$registeredMappings[] = [
'mime' => $mappingClass::getMimeType(),
'extension' => $mappingClass::getExtension(),
'mime' => $mappingClass::getMimeType(),
'extension' => $mappingClass::getExtension(),
'magicBytes' => $mappingClass::getMagicBytes(),
];
}
Expand Down
10 changes: 5 additions & 5 deletions src/Repositories/FileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function moveUploadedFileToTemp(UploadedFileInterface $upload): ?Upload
* Fatal error: Uncaught Laminas\HttpHandlerRunner\Exception\EmitterException:
* Output has been emitted previously; cannot emit response
*/
$tempFile = @tempnam($this->path . '/tmp', 'fof.upload.');
$tempFile = @tempnam($this->path.'/tmp', 'fof.upload.');
$upload->moveTo($tempFile);

$file = new Upload(
Expand Down Expand Up @@ -248,10 +248,10 @@ public function matchFilesForPost(Post $post): void

File::query()
// Files already mapped to the post.
->whereHas('posts', fn($query) => $query->where('posts.id', $post->id))
->whereHas('posts', fn ($query) => $query->where('posts.id', $post->id))
// Files found in (new) content.
->orWhereExists(
fn($query) => $query
fn ($query) => $query
->select($db->raw(1))
->from('posts')
->where('posts.id', $post->id)
Expand Down Expand Up @@ -378,7 +378,7 @@ public function generateFilenameFor(File $file, bool $withFolder = false): strin
{
$today = (new Carbon());

$path = $today->timestamp . '-' . $today->micro . '-' . $file->base_name;
$path = $today->timestamp.'-'.$today->micro.'-'.$file->base_name;

return $withFolder ? sprintf(
'%s%s%s',
Expand Down Expand Up @@ -428,6 +428,6 @@ public function getUrlForFile(File $file): ?string
return null;
}

return $this->getHostnameForFile($file, $adapter) . '/' . $file->path;
return $this->getHostnameForFile($file, $adapter).'/'.$file->path;
}
}

0 comments on commit 461c694

Please sign in to comment.