Skip to content

EPUB imports failing #231

@laboratorijamechtatelja

Description

Describe the bug
EPUB import always fails with "Invalid EPUB file" regardless of the file uploaded, making the EPUB import feature completely non-functional.

To Reproduce

  1. Go to Import EPUB page (/book/import)
  2. Select a valid EPUB file
  3. Select a language
  4. Click Import
  5. See "Invalid EPUB file" error

Expected behavior
The EPUB file is parsed and imported successfully as a book with chapters.

Screenshots
N/A

Server (please complete)

  • LWT version: 3.0.0
  • PHP: 8.4.19
  • Web server: Apache 2.4.66 (Docker)
  • OS: Ubuntu 24.04.3 Live Server (amd64)

Desktop (if necessary):

  • Running on Ubuntu server

Additional context
The root cause is in EpubParserService::isValidEpub(). It validates the file extension of the uploaded file's temporary path ($_FILES['thefile']['tmp_name']) rather than the original filename ($_FILES['thefile']['name']). PHP stores uploaded files in a system temp directory with names like /tmp/phpXXXXXX which have no extension, so the extension check always fails and returns false before the file is ever read.

// isValidEpub() receives the tmp_name path e.g. /tmp/phpXXXXXX
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
if ($extension !== 'epub') {
    return false; // always fails - temp files have no extension
}

Suggested fix: Pass the original filename to isValidEpub() for the extension check while keeping the temp path for the magic byte check:

public function isValidEpub(string $filePath, string $originalName = ''): bool
{
    $nameToCheck = $originalName !== '' ? $originalName : $filePath;
    $extension = strtolower(pathinfo($nameToCheck, PATHINFO_EXTENSION));
    if ($extension !== 'epub') {
        return false;
    }
    // ... magic byte check unchanged
}

And update the call in ImportEpub::execute():

if (!$this->epubParser->isValidEpub($filePath, $uploadedFile['name'] ?? '')) {

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions