|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -function download($file_source, $file_target) { |
4 |
| - $rh = fopen($file_source, 'rb'); |
5 |
| - $wh = fopen($file_target, 'w+b'); |
6 |
| - if (!$rh || !$wh) { |
7 |
| - return false; |
8 |
| - } |
| 3 | +use com\carlgo11\tempfiles\datastorage\DataStorage; |
9 | 4 |
|
10 |
| - while (!feof($rh)) { |
11 |
| - if (fwrite($wh, fread($rh, 4096)) === FALSE) { |
12 |
| - return false; |
13 |
| - } |
14 |
| - echo ''; |
15 |
| - flush(); |
16 |
| - } |
17 |
| - |
18 |
| - fclose($rh); |
19 |
| - fclose($wh); |
20 |
| - |
21 |
| - return true; |
22 |
| -} |
| 5 | +require __DIR__ . '/src/com/carlgo11/tempfiles/autoload.php'; |
23 | 6 |
|
24 | 7 | function return404() {
|
25 | 8 | $notFoundURL = filter_input(INPUT_ENV, 'TMP_404_URL', FILTER_VALIDATE_URL, ['options' => ['default' => 'https://tempfiles.download/download/?404=1']]);
|
26 | 9 | header($_SERVER['SERVER_PROTOCOL'] . " 404 File Not Found");
|
27 |
| - header("Location: $notFoundURL"); |
28 |
| - exit; |
| 10 | + //header("Location: $notFoundURL"); |
| 11 | + //exit; |
29 | 12 | }
|
30 | 13 |
|
31 | 14 | $url = explode('/', strtoupper($_SERVER['REQUEST_URI']));
|
32 | 15 | $id = filter_var($url[1]);
|
33 | 16 | $password = filter_input(INPUT_GET, "p");
|
34 | 17 |
|
35 |
| -# API Download URL |
36 |
| -$downloadURL = filter_input(INPUT_ENV, 'TMP_API_DOWNLOAD_URL', FILTER_VALIDATE_URL, ['options' => ['default' => 'https://api.tempfiles.download/download/?id=%1$s&p=%2$s']]); |
37 |
| -$d_url = sprintf($downloadURL, $id, $password); |
38 |
| - |
39 |
| -$result = download($d_url, "/tmp/$id.tmp"); |
40 |
| -if (!$result) |
41 |
| - return404(); |
42 |
| - |
43 |
| -// Execute cURL command and get response data |
44 |
| -$response = json_decode(curl_exec(file_get_contents("/tmp/$id.tmp"))); |
| 18 | +$file = DataStorage::getFile($id, $password); |
45 | 19 |
|
46 |
| -unlink("/tmp/$id.tmp"); |
| 20 | +$metadata = $file->getMetaData(); |
| 21 | +$content = base64_encode($file->getContent()); |
47 | 22 |
|
| 23 | +if ($file->getMaxViews()) { // max views > 0 |
| 24 | + if ($file->getMaxViews() <= $file->getCurrentViews() + 1) DataStorage::deleteFile($id); |
| 25 | + else $file->setCurrentViews($file->getCurrentViews() + 1); |
| 26 | +} |
48 | 27 |
|
| 28 | +// Set headers |
| 29 | +header("Content-Description: File Transfer"); |
| 30 | +header("Expires: 0"); |
| 31 | +header("Pragma: public"); |
| 32 | +header("Content-Type: {$metadata['type']}"); |
| 33 | +header("Content-Disposition: inline; filename=\"{$metadata['name']}\""); |
| 34 | +header("Content-Length: {$metadata['size']}"); |
49 | 35 |
|
50 |
| -if ($response->data) { |
51 |
| - |
52 |
| - // Set headers |
53 |
| - header("Content-Description: File Transfer"); |
54 |
| - header("Expires: 0"); |
55 |
| - header("Pragma: public"); |
56 |
| - header("Content-Type: {$response->type}"); |
57 |
| - header("Content-Disposition: inline; filename=\"{$response->filename}\""); |
58 |
| - header("Content-Length: {$response->length}"); |
59 |
| - |
60 |
| - // output file contents |
61 |
| - echo base64_decode($response->data); |
62 |
| - |
63 |
| -} else return404(); |
64 |
| -exit; |
| 36 | +// output file contents |
| 37 | +echo base64_decode($content); |
0 commit comments