send2ereader

repository·master·Indexed 21 days ago

https://github.com/daniel-j/send2ereader

A self-hostable service for sending ebooks to Kobo and Kindle e-readers via their built-in web browsers. It supports automatic conversions using Kepubify, KindleGen, and pdfCropMargins, and provides a RESTful API for session key generation, file uploads, and downloads. The service can be run directly on a host OS with Node.js (version 16 or 20) or deployed using Docker Compose.

Tokens
1.8K
Snippets
8
Records
9
Agent score
76%

What's inside send2ereader

  1. How file expiration and cleanup works

    master

    The service manages file lifecycles using a combination of timers and manual deletions:

    1. Key Expiration: When a key is generated, a timer is set for expireDelay (30 seconds). If no activity occurs, the key is removed from memory. If the session persists, the key can live up to maxExpireDuration (1 hour).
    2. File Cleanup: When a new file is uploaded to an existing key, the previous file associated with that key is deleted from the uploads/ directory.
    3. Manual Deletion: Calling DELETE /file/:key immediately clears the file association for that key.
    4. Automatic Cleanup: If a conversion process (like kindlegen) fails, the service attempts to delete the temporary input and output files.
  2. Run send2ereader using Docker Compose

    master

    You can run send2ereader in a containerized environment using Docker and Docker Compose. Ensure you have cloned the repository so that the Dockerfile, docker-compose.yaml, and package.json are in your current working directory.

    1. Build the image: docker compose build
    2. Start the container in detached mode: docker compose up -d
    3. Access the service at http://localhost:3001
    git clone https://github.com/daniel-j/send2ereader.git
    cd send2ereader
    docker compose build
    docker compose up -d
  3. Install and run send2ereader on your host OS

    master

    To run send2ereader directly on your operating system, ensure you have Node.js (version 16 or 20) installed and that the following external dependencies are installed and available in your system's PATH:

    • Kepubify: For Kobo ebook processing.
    • KindleGen: For Kindle ebook processing.
    • pdfCropMargins: For PDF processing.

    Follow these steps:

    1. Install dependencies: npm install
    2. Start the service: npm start
    3. Access the service at http://localhost:3001
    npm install
    npm start
  4. Deploy send2ereader using Docker Compose

    master

    You can deploy send2ereader using Docker Compose. The configuration defines a service named send2ereader that builds from the local directory using the provided Dockerfile.

    Configuration Details

    • Restart Policy: The container is configured with restart: unless-stopped, meaning it will automatically restart after a shutdown unless you manually stop the container.
    • Port Mapping: The service binds an external port to the internal container port. The default mapping is 3001:3001. If you need to change the access port, only modify the external port (the first number in the pair).

    Format: <External Port>:<Internal Port>

    services:
      send2ereader:
        build:
          context: .
          dockerfile: ./Dockerfile
        container_name: send2ereader
        restart: unless-stopped
        ports:
          - 3001:3001
  5. API Reference: send2ereader HTTP Endpoints

    master

    The send2ereader service provides a RESTful API for generating session keys, uploading files with optional conversions, checking status, and downloading files. All keys are case-insensitive (converted to uppercase internally).

    ### Endpoints Summary
    
    | Method | Path | Description |
    | :--- | :--- | :--- |
    | `POST` | `/generate` | Generates a unique 4-character session key. Returns the key as plain text. |
    | `POST` | `/upload` | Uploads a file associated with a `key`. Supports optional conversion flags. |
    | `GET` | `/status/:key` | Returns the status of a session, including file info and associated URLs. |
    | `DELETE` | `/file/:key` | Clears the file associated with the given key. |
    | `GET` | `/:filename` | Downloads the file associated with a specific session key. |
    | `GET` | `/receive` | Serves the `download.html` interface. |
    | `GET` | `/` | Serves either `upload.html` or `download.html` based on the `User-Agent`. |
  6. Check session status via GET /status/:key

    master

    Retrieve the current state of a session key. This is useful for verifying if a file has been successfully processed and uploaded.

    Returns:

    • alive: Timestamp of last activity.
    • file: Object containing name (if a file is present).
    • urls: Array of URLs associated with this session.
    # Example: Checking status
    curl http://localhost:3001/status/ABCD
  7. Download a file via GET /:filename

    master

    To download a file, you must provide the session key as a query parameter. The filename in the URL path must match the filename stored in the session.

    Note: The User-Agent used for the download must match the User-Agent used during the upload/generation phase.

    # Example: Downloading a file
    # If the file name is 'mybook.epub' and key is 'ABCD'
    curl "http://localhost:3001/mybook.epub?key=ABCD"
  8. Upload files and trigger conversions via POST /upload

    master

    Upload a file using multipart/form-data. You must include the key in the request body. The server supports several automatic conversions based on the User-Agent and specific request body flags:

    • Kindle Conversion: If User-Agent contains Kindle and the body includes kindlegen=true, .epub files are converted to .mobi using the kindlegen binary.
    • Kobo Conversion: If User-Agent contains Kobo and the body includes kepubify=true, .epub files are converted to .kepub.epub using the kepubify binary.
    • PDF Cropping: If the file is a .pdf and the body includes pdfcropmargins=true, the margins are cropped using the pdfcropmargins binary.
    • Transliteration: If the body includes transliteration=true, the filename is transliterated and sanitized.

    Allowed File Types:

    • epub, mobi, pdf, cbz, cbr, html, txt, zip, rar.
    # Example: Uploading an epub for Kindle conversion
    curl -X POST http://localhost:3001/upload \
      -F "key=ABCD" \
      -F "file=@book.epub" \
      -F "kindlegen=true"
  9. Generate a session key via POST /generate

    master

    To start a session, call the /generate endpoint. The server generates a unique 4-character key (using characters 23456789ACDEFGHJKLMNPRSTUVWXYZ) and sets a key cookie. The key is valid for 30 seconds for the initial upload phase, but the session can last up to 1 hour if activity continues.

    # Example using curl to get a key
    curl -X POST http://localhost:3001/generate