Piotr Labudda 9 лет назад
Родитель
Сommit
7028901240
1 измененных файлов с 42 добавлено и 0 удалено
  1. 42 0
      SE/se-lib/Route/FileStorage.php

+ 42 - 0
SE/se-lib/Route/FileStorage.php

@@ -205,6 +205,43 @@ class Route_FileStorage extends RouteBase {
 		}
 	}
 
+	public function downloadAction() {
+		try {
+			$id = V::get('id', 0, $_REQUEST, 'int');
+			if ($id <= 0) throw new Exception("Error Wrong file id");
+
+			$sqlId = DB::getPDO()->quote($id, PDO::PARAM_INT);
+			$sqlTblName = $this->getTableName();
+			$rows = DB::getPDO()->fetchAll("
+				select f.*
+				from `{$sqlTblName}` f
+				where f.ID = {$sqlId}
+			");
+			DBG::_('DBG', '>2', "rows", $rows, __CLASS__, __FUNCTION__, __LINE__);
+			if (empty($rows)) throw new HttpException("Plik nie istnieje", 404);
+			$file = $rows[0];
+			// TODO: check perms
+			$fileName = V::get('FILE_LABEL', $id, $file);
+			$rootFileStoragePath = $this->getRootStoragePath();
+			$filePath = $this->generateFilePathHashFromID($file['ID']);
+			$absFilePath = "{$rootFileStoragePath}/{$filePath}";
+			if (!file_exists($absFilePath)) throw new Exception("file not exists!");
+			header('Content-Description: File Transfer');
+			$fileMimeType = ($file['FILE_SIZE']) ? $file['FILE_SIZE'] : filesize($absFilePath);
+			header('Content-Type: ' . $fileMimeType);
+			header('Content-Disposition: attachment; filename="' . $fileName . '"');
+			header('Expires: 0');
+			header('Cache-Control: must-revalidate');
+			header('Pragma: public');
+			$fileSize = ($file['FILE_MIME_TYPE']) ? $file['FILE_MIME_TYPE'] : filesize($absFilePath);
+			header('Content-Length: ' . $fileSize);
+			readfile($absFilePath);
+			exit;
+		} catch (Exception $e) {
+			echo "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage();
+		}
+	}
+
 	public function downloadTestAction() {
 		try {
 			$filePath = $this->getRootStoragePath() . '/test-upload-data.txt';
@@ -290,6 +327,11 @@ foreach ($rows as $idx => $row) {
 $params = array();
 $params['caption'] = "Files in '" . $this->getTableName() . "'";
 $params['rows'] = $rows;
+foreach ($params['rows'] as $idx => $row) {
+	$downloadLink = "index.php?_route=FileStorage&_task=download&id={$row['ID']}";
+	$row['download'] = '<a href="' . $downloadLink . '" target="_blank">download</a>';
+	$params['rows'][$idx] = $row;
+}
 UI::table($params);
 ?>
 <script>