Page MenuHomePhorge

chwala and seafile on a hidden server [patch]
Closed, ResolvedPublic

Description

I run Kolab 3.4 on a server only accessible from the local network. For remote access I run a smaller
roundcube instance with only some plugins on an internet exposed host. For file access I use:

$config['fileapi_backend'] = 'kolab';
$config['fileapi_drivers'] = array('seafile');
$config['fileapi_seafile_host'] = 'kolab3.example.org/seafile/';

That seems to work - in roundcube with kolab_files enabled I see my seafile libraries, but when I try
to access a file, the browser wants to load the file directly from the seafile host which is not exposed.

Some reading in chwala led me to chwala/lib/drivers/seafile/seafile_file_storage.php, ~ line 500:

// just send redirect to SeaFile server
 if ($file['size']) {
    header("Location: $link");

That won't work for me. The following patch seems to work:

--- chwala/lib/drivers/seafile/seafile_file_storage.php.orig    2017-01-21 15:21:24.647750300 +0100
+++ chwala/lib/drivers/seafile/seafile_file_storage.php 2017-01-21 15:46:06.883436733 +0100
@@ -505,9 +505,28 @@
         header("Content-Length: " . $file['size']);
         header("Content-Disposition: $disposition; filename=\"$filename\"");
 
-        // just send redirect to SeaFile server
+        // proxy the file from the seafile server
         if ($file['size']) {
-            header("Location: $link");
+            $myoptions = array(
+                CURLOPT_RETURNTRANSFER => true,     // return web page
+                CURLOPT_HEADER         => false,     // return headers
+                CURLOPT_FOLLOWLOCATION => true,     // follow redirects
+                CURLOPT_ENCODING       => "",       // handle all encodings
+                CURLOPT_AUTOREFERER    => true,     // set referer on redirect
+                CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
+                CURLOPT_TIMEOUT        => 120,      // timeout on response
+                CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
+            );
+
+            $ch      = curl_init( $link );
+            curl_setopt_array( $ch, $myoptions );
+            $remoteSite = curl_exec( $ch );
+            //$header  = curl_getinfo( $ch );
+            curl_close( $ch );
+
+            //$header['content'] = $remoteSite;
+
+            echo($remoteSite);
         }
         die;
     }

Details

Ticket Type
Task

Event Timeline

jh23453 renamed this task from chwala and seafile on a hidden server to chwala and seafile on a hidden server [patch].Jan 21 2017, 3:55 PM
jh23453 added a project: Chwala.
jh23453 updated the task description. (Show Details)

So, this would load the whole file into memory. Not something we'd like to do. So, first of all this probably should be an optional feature. Second, we should use seafile_request_observer to proxy the file content directly to the client. See seafile_file_storage::save_file_content(). Would you like to create a differential?

Yes, that'll load the file into memory - no problem for my users, because our files are small.

I'll try to have a look at save_file_content(), but my php skills are
quite limited.

Right now I have a kolab server with seafile and access it from another roundcube instance (and chwala on that host too). I'll also try to remove the externally accessible chwala.

That's far easier than I thought. We already use save_file_content for special condition.
Let's just do that instead of sending the "Location:"-header too. This patch replaces
to one above. Would that work?

--- chwala/lib/drivers/seafile/seafile_file_storage.php.orig    2017-01-26 18:46:37.149214751 +0100
+++ chwala/lib/drivers/seafile/seafile_file_storage.php 2017-03-12 10:05:37.751389351 +0100
@@ -505,21 +505,16 @@
         header("Content-Length: " . $file['size']);
         header("Content-Disposition: $disposition; filename=\"$filename\"");
 
-        // just send redirect to SeaFile server
+        // proxy the file from the seafile server
         if ($file['size'] && empty($params['head'])) {
             // In view-mode we can't redirect to SeaFile server because:
             // - it responds with Content-Disposition: attachment, which causes that
             //   e.g. previewing images is not possible
             // - pdf/odf viewers can't follow redirects for some reason (#4590)
-            if (empty($params['force-download'])) {
-                if ($fp = fopen('php://output', 'wb')) {
-                    $this->save_file_content($link, $fp);
-                    fclose($fp);
-                    die;
-                }
+            if ($fp = fopen('php://output', 'wb')) {
+                $this->save_file_content($link, $fp);
+                fclose($fp);
             }
-
-            header("Location: $link");
         }
     }
machniak claimed this task.