titleMimeType() static public method

To reduce search time the function checks first wether the file has a well known extension. If not two functions are tried. If all fails 'application/force-download' is returned to force the download of the unknown format.
static public titleMimeType ( string $file_path ) : string
$file_path string path to ebook file
return string MIME type
Exemplo n.º 1
0
 function testTitleMimeType()
 {
     $this->assertEqual('application/epub+zip', Utilities::titleMimeType('x/y/test.epub'));
     $this->assertEqual('application/vnd.amazon.ebook', Utilities::titleMimeType('test.azw'));
     $this->assertEqual('application/x-mobipocket-ebook', Utilities::titleMimeType('test.mobi'));
     $this->assertEqual('text/plain', Utilities::titleMimeType(self::FIXT . '/test.unknown-format'));
     $this->assertEqual('application/xml', Utilities::titleMimeType(self::FIXT . '/atom.rng'));
 }
Exemplo n.º 2
0
 /**
  * Write a catalog entry for a book title with acquisition links
  * @param  array    $entry      the book and its details 
  * @param  boolean  $protected  true = use an indirect acquisition link, 
  *                              else a direct one 
  */
 function partialAcquisitionEntry($entry, $protected)
 {
     $titleLink = $this->bbs_root . '/opds/titles/' . $entry['book']->id;
     $this->xmlw->startElement('entry');
     $this->xmlw->writeElement('id', 'urn:bicbucstriim:' . $titleLink);
     $this->xmlw->writeElement('title', $entry['book']->title);
     $this->xmlw->writeElement('dc:issued', date("Y", strtotime($entry['book']->pubdate)));
     $this->xmlw->writeElement('updated', $this->updated);
     $this->xmlw->startElement('author');
     $this->xmlw->writeElement('name', $entry['book']->author_sort);
     $this->xmlw->endElement();
     $this->xmlw->startElement('content');
     $this->xmlw->writeAttribute('type', 'text/html');
     $this->xmlw->text($entry['comment']);
     $this->xmlw->endElement();
     $this->xmlw->startElement("dc:language");
     $this->xmlw->text($entry['language']);
     $this->xmlw->endElement();
     if (isset($entry['book']->thumbnail) && $entry['book']->thumbnail) {
         $tlink = $this->bbs_root . '/data/titles/thumb_' . $entry['book']->id . '.png';
     } else {
         $tlink = $titleLink . '/thumbnail/';
     }
     $this->thumbnailLink($tlink);
     $this->imageLink($titleLink . '/cover/');
     #$this->detailsLink($titleLink.'/thumbnail/');
     foreach ($entry['formats'] as $format) {
         $fname = $format->name;
         $ext = strtolower($format->format);
         $bp = Utilities::bookPath($this->calibre_dir, $entry['book']->path, $fname . '.' . $ext);
         $mt = Utilities::titleMimeType($bp);
         if ($protected) {
             $this->indirectDownloadLink($titleLink . '/showaccess/', $mt);
         } else {
             $this->directDownloadLink($titleLink . '/file/' . urlencode($fname) . '.' . $ext, $mt);
         }
     }
     foreach ($entry['tags'] as $category) {
         $this->xmlw->startElement('category');
         $this->xmlw->writeAttribute('term', $category->name);
         $this->xmlw->writeAttribute('label', $category->name);
         $this->xmlw->endElement();
     }
     $this->xmlw->endElement();
 }
Exemplo n.º 3
0
function book($id, $file)
{
    global $app, $globalSettings;
    // parameter checking
    if (!is_numeric($id)) {
        $app->getLog()->warn('book: invalid title id ' . $id);
        $app->halt(400, "Bad parameter");
    }
    // TODO check file parameter?
    $details = $app->calibre->titleDetails($globalSettings['lang'], $id);
    if (is_null($details)) {
        $app->getLog()->warn("book: no book found for " . $id);
        $app->notFound();
    }
    // for people trying to circumvent filtering by direct access
    if (title_forbidden($details)) {
        $app->getLog()->warn("book: requested book not allowed for user: "******"book download by " . $app->auth->getUserName() . " for " . $real_bookpath . " with metadata update = " . $globalSettings[METADATA_UPDATE]);
    if ($contentType == Utilities::MIME_EPUB && $globalSettings[METADATA_UPDATE]) {
        if ($details['book']->has_cover == 1) {
            $cover = $app->calibre->titleCover($id);
        } else {
            $cover = null;
        }
        // If an EPUB update the metadata
        $mdep = new MetadataEpub($real_bookpath);
        $mdep->updateMetadata($details, $cover);
        $bookpath = $mdep->getUpdatedFile();
        $app->getLog()->debug("book(e): file " . $bookpath);
        $app->getLog()->debug("book(e): type " . $contentType);
        $booksize = filesize($bookpath);
        $app->getLog()->debug("book(e): size " . $booksize);
        if ($booksize > 0) {
            header("Content-Length: " . $booksize);
        }
        header("Content-Type: " . $contentType);
        header("Content-Disposition: attachment; filename=\"" . $file . "\"");
        header("Content-Description: File Transfer");
        header("Content-Transfer-Encoding: binary");
        readfile_chunked($bookpath);
    } else {
        // Else send the file as is
        $bookpath = $real_bookpath;
        $app->getLog()->debug("book: file " . $bookpath);
        $app->getLog()->debug("book: type " . $contentType);
        $booksize = filesize($bookpath);
        $app->getLog()->debug("book: size " . $booksize);
        header("Content-Length: " . $booksize);
        header("Content-Type: " . $contentType);
        header("Content-Disposition: attachment; filename=\"" . $file . "\"");
        header("Content-Description: File Transfer");
        header("Content-Transfer-Encoding: binary");
        readfile_chunked($bookpath);
    }
}