View Single Post
Old 09-18-2013, 11:42 AM   #611
zitronenrollchen
Junior Member
zitronenrollchen began at the beginning.
 
Posts: 3
Karma: 10
Join Date: Feb 2013
Device: Kindle NT
Using COPS with library in Dropbox

I have the following setup:

1. COPS and my metadata.db are hosted in my webspace
2. The calibre library is in a local folder that syncs with dropbox. I do not want so have a copy in my webspace.

So to have ebooks and covers available in COPS I have to rewrite all links and also cache cover photos in my webspace. I access all files in dropbox with the following perl script (cgi-bin) that uses the dropbox api.

Code:
#!/usr/bin/perl

use strict;
use Net::Dropbox::API;
use URI::Escape;
use CGI;

my $types = {
    epub => 'application/epub+zip',
    mobi => 'application/x-mobipocket-ebook',
    pdf => 'application/pdf',
    jpg => 'image/jpeg',
};

my $cfg;
my $dev_key = 'xxxxxx';
my $dev_secret = 'xxxxxxx';

### config 
$cfg->{access_token} = 'xxxxx';
$cfg->{access_secret} = 'xxxxx';

my $base = 'ebooks';
my $q = CGI->new();
my $f = $q->path_info();
my ($ext) = ($f =~ /\.(\w+)$/);

## authenticate
my $box = Net::Dropbox::API->new({key => $dev_key, secret => $dev_secret});
$box->access_token($cfg->{access_token});
$box->access_secret($cfg->{access_secret});

$box->context('dropbox'); ## or 'sandbox'
my $file = $box->getfile($base . join("/", map { uri_escape($_) } split(/\//, $f)));

if ($file =~ /^{"http_response_code":"(\d+)"/)
{
    error($1);
}
else
{
    print $q->header($types->{$ext} || 'application/octet-stream');
    print $file;
}

exit;

sub error
{
    my $error = shift;
    print $q->header(-type=>"text/plain", -status=>$error);
    print "error $error";
    exit 1;
}
I had to make a couple of changes to COPS (0.6.1 but diff applies also to 0.6.2) to reflect my needs.

Code:
diff -urN ../ebook.orig/book.php ./book.php
--- ../ebook.orig/book.php      2013-07-29 14:03:58.000000000 +0200
+++ ./book.php  2013-08-27 11:32:57.889844409 +0200
@@ -83,7 +83,7 @@
         $this->comment = $line->comment;
         $this->uuid = $line->uuid;
         $this->hasCover = $line->has_cover;
-        if (!file_exists ($this->getFilePath ("jpg"))) {
+        if (!$config['cops_alex_ebook_link_prefix'] && !file_exists ($this->getFilePath ("jpg"))) {
             // double check
             $this->hasCover = 0;
         }
@@ -562,4 +562,27 @@
         return $entryArray;
     }
 
+    public function get_remote_link ($type, $bookId) {
+       global $config;
+       $file = $this->getFilePath ($type);
+       if (! $config['cops_alex_cover_remote_link_prefix'] ) return $file;
+
+       $cache = $config['cops_alex_cover_cache_prefix'].$bookId.".jpg";
+       if ($bookId && file_exists ($cache)) { return $cache; }
+
+       $file = $config['cops_alex_cover_remote_link_prefix'] . str_replace('%2F','/', rawurlencode($file));
+       $ch = curl_init($file);
+       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+       $curl_data = curl_exec($ch);
+       if (curl_errno($ch) == 0) {
+         if ($fd = @fopen($config['cops_alex_cover_cache_prefix'].$bookId.".jpg", "wb")) {
+            fwrite($fd, $curl_data);
+            fclose($fd);
+            $file = $config['cops_alex_cover_cache_prefix'].$bookId.".jpg";
+        }
+       }
+       return $file;
+   }
+
+
 }
diff -urN ../ebook.orig/data.php ./data.php
--- ../ebook.orig/data.php      2013-07-22 14:07:38.000000000 +0200
+++ ./data.php  2013-08-27 10:11:29.057281377 +0200
@@ -139,7 +139,7 @@
         }
         else
         {
-            return new Link (str_replace('%2F','/',rawurlencode ($book->path."/".$filename)), $mime, $rel, $title);
+            return new Link (str_replace('%2F','/',$config['cops_alex_ebook_link_prefix'].rawurlencode ($book->path."/".$filename)), $mime, $rel, $title);
         }
     }
 }
diff -urN ../ebook.orig/fetch.php ./fetch.php
--- ../ebook.orig/fetch.php     2013-07-22 14:10:50.000000000 +0200
+++ ./fetch.php 2013-08-27 11:38:34.069344458 +0200
@@ -45,7 +45,7 @@
         } else {
             $file = $book->getFilePath ($type, $idData);
         }
-        if (!$file || !file_exists ($file)) {
+        if (!$config['cops_alex_cover_remote_link_prefix'] && (!$file || !file_exists ($file))) {
             notFound ();
             return;
         }
@@ -57,7 +57,8 @@
             header("Content-Type: image/jpeg");
             if (isset($_GET["width"]))
             {
-                $file = $book->getFilePath ($type);
+                //$file = $book->getFilePath ($type);
+               $file = $book->get_remote_link($type, $bookId);
                 // get image size
                 if($size = GetImageSize($file)){
                     $w = $size[0];
@@ -83,7 +84,8 @@
             }
             if (isset($_GET["height"]))
             {
-                $file = $book->getFilePath ($type);
+                //$file = $book->getFilePath ($type);
+               $file = $book->get_remote_link($type, $bookId);
                 // get image size
                 if($size = GetImageSize($file)){
                     $w = $size[0];
@@ -119,6 +121,7 @@
         return;
     }
     if ($type == "jpg") {
+        $file = $book->get_remote_link($type, $bookId);
         header('Content-Disposition: filename="' . basename ($file) . '"');
     } else {
         header('Content-Disposition: attachment; filename="' . basename ($file) . '"');
@@ -131,6 +134,7 @@
     
     if (empty ($config['cops_x_accel_redirect'])) {
         $filename = $dir . $file;
+       if ($config['cops_alex_cover_remote_link_prefix']) { $filename = $file; }
         $fp = fopen($filename, 'rb');
         header("Content-Length: " . filesize($filename));
         fpassthru($fp);
These are the new config options:

Code:
$config['cops_alex_ebook_link_prefix'] = "/cgi-bin/ebook/drop.pl/calibre-books/";
$config['cops_alex_cover_remote_link_prefix'] = "http://my.server.com/cgi-bin/ebook/drop.pl/calibre-books/";
$config['cops_alex_cover_cache_prefix'] = "/tmp/cops6ebook";
Maybe this is of any help to some of you. I would like to see something like this in a future version of COPS if there is any interest.

This is all very quick and dirty.

Thanks for COPS.
Alex
zitronenrollchen is offline   Reply With Quote