Browse Source

Stream binary files, fixes #5

Sunny 2 years ago
parent
commit
24191a8de7
2 changed files with 20 additions and 17 deletions
  1. 7 6
      pages/audio.php
  2. 13 11
      pages/image.php

+ 7 - 6
pages/audio.php

@@ -1,10 +1,11 @@
 <?php
-  $audio = "https://t4.bcbits.com/stream/" . urlencode($_GET["directory"]) . "/" . urlencode($_GET["format"]) . "/" . urlencode($_GET["file"]) . "?token=" . urlencode($_GET["token"]);
-  $audio = file_get_contents($audio);
+  $ch = curl_init("https://t4.bcbits.com/stream/" . urlencode($_GET["directory"]) . "/" . urlencode($_GET["format"]) . "/" . urlencode($_GET["file"]) . "?token=" . urlencode($_GET["token"]));
 
-  $mime = new finfo(FILEINFO_MIME_TYPE);
-  $mime = $mime->buffer($audio);
+  curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) {
+    echo $data;
+    return strlen($data);
+  });
 
-  header("Content-Type: " . $mime);
-  echo $audio;
+  header("Content-Type: application/octet-stream");
+  curl_exec($ch);
 ?>

+ 13 - 11
pages/image.php

@@ -5,19 +5,21 @@
   ];
 
   foreach ($images as $image) {
-    $context = stream_context_create([
-      "http" => [
-        "ignore_errors" => true
-      ]
-    ]);
-    $image = file_get_contents($image, false, $context);
+    $ch = curl_init($image);
 
-    if (explode(" ", $http_response_header[0])[1] === "200") {
-      $mime = new finfo(FILEINFO_MIME_TYPE);
-      $mime = $mime->buffer($image);
+    curl_setopt($ch, CURLOPT_NOBODY, true);
 
-      header("Content-Type: " . $mime);
-      echo $image;
+    curl_exec($ch);
+
+    if (curl_getinfo($ch, CURLINFO_RESPONSE_CODE) === 200) {
+      curl_setopt($ch, CURLOPT_NOBODY, false);
+      curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) {
+        echo $data;
+        return strlen($data);
+      });
+
+      header("Content-Type: application/octet-stream");
+      curl_exec($ch);
 
       break;
     };