Explorar o código

Ticket #3350: Files downloading enabled for Files module only

Alexander %!s(int64=5) %!d(string=hai) anos
pai
achega
23de64f24f

+ 2 - 1
inc/classes/BxDolStorage.php

@@ -650,8 +650,9 @@ abstract class BxDolStorage extends BxDolFactory implements iBxDolFactoryObject
     /**
      * Download file.
      * @param array $aFile downloading file info.
+     * @param boolean $bForceDownloadDialog if downloading to a local file system first is required and/or send the outout as attachment rather than inline.
      */
-    public function download ($aFile)
+    public function download ($aFile, $sToken = false, $bForceDownloadDialog = false)
     {
     	$bRet = true;
         bx_alert($this->_aObject['object'], 'file_downloaded', $aFile['id'], bx_get_logged_profile_id(), array(

+ 3 - 3
inc/classes/BxDolStorageLocal.php

@@ -44,9 +44,9 @@ class BxDolStorageLocal extends BxDolStorage
     }
 
     /**
-     * Start file fownloading by remote id. If file is private then token is checked.
+     * Start file downloading by remote id. If file is private then token is checked.
      */
-    public function download ($sRemoteId, $sToken = false)
+    public function download ($sRemoteId, $sToken = false, $bForceDownloadDialog = false)
     {
         $this->setErrorCode(BX_DOL_STORAGE_ERR_OK);
 
@@ -66,7 +66,7 @@ class BxDolStorageLocal extends BxDolStorage
             return false;
         }
 
-        if (!bx_smart_readfile($sFileLocation, $aFile['file_name'], $aFile['mime_type'], $aFile['private'] && $this->_iCacheControl > $this->_aObject['token_life'] ? $this->_aObject['token_life'] : $this->_iCacheControl, $aFile['private'] ? 'private' : 'public', 'attachment')) {
+        if (!bx_smart_readfile($sFileLocation, $aFile['file_name'], $aFile['mime_type'], $aFile['private'] && $this->_iCacheControl > $this->_aObject['token_life'] ? $this->_aObject['token_life'] : $this->_iCacheControl, $aFile['private'] ? 'private' : 'public', $bForceDownloadDialog ? 'attachment' : 'inline')) {
             $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
             return false;
         }

+ 14 - 11
inc/classes/BxDolStorageS3.php

@@ -77,7 +77,7 @@ class BxDolStorageS3 extends BxDolStorage
     /**
      * Start file downloading by remote id. If file is private then token is checked.
      */
-    public function download ($sRemoteId, $sToken = false)
+    public function download ($sRemoteId, $sToken = false, $bForceDownloadDialog = false)
     {
         $this->setErrorCode(BX_DOL_STORAGE_ERR_OK);
 
@@ -94,17 +94,20 @@ class BxDolStorageS3 extends BxDolStorage
 
         $sUrl = $this->getFileUrlById($aFile['id']);
 
+        if ($bForceDownloadDialog) {
+            // download remote file to tmp
+            $sTmpFilePath = BX_DIRECTORY_PATH_TMP . 'dwnld_'.$sRemoteId;
+            if (!file_exists($sTmpFilePath)) {
+                @file_put_contents($sTmpFilePath, bx_file_get_contents($sUrl));
+            }
 
-        // download remote file to tmp
-        $sTmpFilePath = BX_DIRECTORY_PATH_TMP . 'dwnld_'.$sRemoteId;
-        if (!file_exists($sTmpFilePath)) {
-            @file_put_contents($sTmpFilePath, bx_file_get_contents($sUrl));
-        }
-
-        // read from a local storage to be able to send it as attahment and give it a proper name
-        if (!file_exists($sTmpFilePath) || !bx_smart_readfile($sTmpFilePath, $aFile['file_name'], $aFile['mime_type'], $aFile['private'] && $this->_iCacheControl > $this->_aObject['token_life'] ? $this->_aObject['token_life'] : $this->_iCacheControl, $aFile['private'] ? 'private' : 'public', 'attachment')) {
-            $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
-            return false;
+            // read from a local storage to be able to send it as attachment and give it a proper name
+            if (!file_exists($sTmpFilePath) || !bx_smart_readfile($sTmpFilePath, $aFile['file_name'], $aFile['mime_type'], $aFile['private'] && $this->_iCacheControl > $this->_aObject['token_life'] ? $this->_aObject['token_life'] : $this->_iCacheControl, $aFile['private'] ? 'private' : 'public', 'attachment')) {
+                $this->setErrorCode(BX_DOL_STORAGE_ERR_ENGINE_GET);
+                return false;
+            }
+        } else {
+            header("Location: " . $sUrl);
         }
 
         return parent::download($aFile);

+ 1 - 1
modules/boonex/files/classes/BxFilesModule.php

@@ -113,7 +113,7 @@ class BxFilesModule extends BxBaseModTextModule
             return;
         }
 
-        if (!$oStorage->download($aFile['remote_id'], $sToken)) {
+        if (!$oStorage->download($aFile['remote_id'], $sToken, true)) {
             $this->_oTemplate->displayErrorOccured();
             return;
         }