Commit 9346e787 authored by Yassine Doghri's avatar Yassine Doghri
Browse files

fix(media): copy and delete temp file when saving instead of moving it for FS FileManager

+ throw exception instead silently failing file save

closes #338
parent 0775add6
Loading
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ namespace Modules\Media\Entities;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Files\File;
use Modules\Media\Models\MediaModel;
use RuntimeException;

/**
 * @property int $id
@@ -97,15 +98,13 @@ class BaseMedia extends Entity
        return $this;
    }

    public function saveFile(): bool
    public function saveFile(): void
    {
        if (! $this->attributes['file'] || ! $this->file_key) {
            return false;
            throw new RuntimeException("'file' and 'file_key' attributes must be set before saving a file.");
        }

        $this->attributes['file_key'] = service('file_manager')->save($this->attributes['file'], $this->file_key);

        return true;
    }

    public function deleteFile(): bool
@@ -128,6 +127,7 @@ class BaseMedia extends Entity

        if (! service('file_manager')->rename($this->file_key, $newFileKey)) {
            $db->transRollback();

            return false;
        }

+2 −2
Original line number Diff line number Diff line
@@ -94,14 +94,14 @@ class Image extends BaseMedia
        return $this;
    }

    public function saveFile(): bool
    public function saveFile(): void
    {
        if ($this->attributes['sizes'] !== []) {
            $this->initFileProperties();
            $this->saveSizes();
        }

        return parent::saveFile();
        parent::saveFile();
    }

    public function deleteFile(): bool
+7 −9
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Modules\Media\Entities;

use CodeIgniter\Files\File;
use Exception;
use Modules\Media\TranscriptParser;

class Transcript extends BaseMedia
@@ -53,11 +54,11 @@ class Transcript extends BaseMedia
        return $this;
    }

    public function saveFile(): bool
    public function saveFile(): void
    {
        $this->saveJsonTranscript();

        return parent::saveFile();
        parent::saveFile();
    }

    public function deleteFile(): bool
@@ -73,19 +74,18 @@ class Transcript extends BaseMedia
        return true;
    }

    private function saveJsonTranscript(): bool
    private function saveJsonTranscript(): void
    {
        $srtContent = file_get_contents($this->file->getRealPath());

        $transcriptParser = new TranscriptParser();

        if ($srtContent === false) {
            return false;
            throw new Exception('Could not read transcript file at ' . $this->file->getRealPath());
        }

        if (! $transcriptJson = $transcriptParser->loadString($srtContent)->parseSrt()) {
            return false;
        }
        $transcriptJson = $transcriptParser->loadString($srtContent)
            ->parseSrt();

        $tempFilePath = WRITEPATH . 'uploads/' . $this->file->getRandomName();
        file_put_contents($tempFilePath, $transcriptJson);
@@ -94,7 +94,5 @@ class Transcript extends BaseMedia

        service('file_manager')
            ->save($newTranscriptJson, $this->json_key);

        return true;
    }
}
+15 −15
Original line number Diff line number Diff line
@@ -20,32 +20,32 @@ class FS implements FileManagerInterface
    /**
     * Saves a file to the corresponding folder in `public/media`
     */
    public function save(File $file, string $path): string | false
    public function save(File $file, string $key): string
    {
        helper('media');

        if ((pathinfo($path, PATHINFO_EXTENSION) === '') && (($extension = $file->getExtension()) !== '')) {
            $path = $path . '.' . $extension;
        }

        $mediaRoot = $this->media_path_absolute();
        $path = $mediaRoot . '/' . $key;

        if (! file_exists(dirname($mediaRoot . '/' . $path))) {
            mkdir(dirname($mediaRoot . '/' . $path), 0777, true);
        if (! file_exists(dirname($path))) {
            mkdir(dirname($path), 0777, true);
        }

        if (! file_exists(dirname($mediaRoot . '/' . $path) . '/index.html')) {
            touch(dirname($mediaRoot . '/' . $path) . '/index.html');
        if (! file_exists(dirname($path) . '/index.html')) {
            touch(dirname($path) . '/index.html');
        }

        try {
            // move to media folder, overwrite file if already existing
            $file->move($mediaRoot . '/', $path, true);
        } catch (Exception) {
            return false;
        // copy to media folder, overwrite file if already existing
        $isCopySuccessful = copy($file->getRealPath(), $path);

        if (! $isCopySuccessful) {
            throw new Exception("Could not save file {$key} to {$path}");
        }

        return $path;
        // delete temporary file after copy
        unlink($file->getRealPath());

        return $key;
    }

    public function delete(string $key): bool
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ use CodeIgniter\HTTP\Response;

interface FileManagerInterface
{
    public function save(File $file, string $key): string | false;
    public function save(File $file, string $key): string;

    public function delete(string $key): bool;

Loading