Пример #1
0
 public function upload()
 {
     $app = App::getInstance();
     $user_id = SessionManager::getInstance()->getUserID();
     if (!empty($_FILES['file']['name'])) {
         $filename = $_FILES['file']['name'];
         $file = sprintf("%s/%s", PathUtils::getTmpDir('tmp/uploads'), $filename);
         $saved = move_uploaded_file($_FILES['file']['tmp_name'], $file);
     } elseif (!empty($_POST['file']) && !empty($_POST['data'])) {
         $filename = $_POST['file'];
         $file = sprintf("%s/%s", PathUtils::getTmpDir('tmp/uploads'), $filename);
         $data = !empty($_POST['base64']) ? base64_decode($_POST['data']) : $_POST['data'];
         $saved = file_put_contents($file, $data);
     }
     if (!empty($file) & !empty($saved)) {
         try {
             $type = PathUtils::getFileType($file);
             $event = new UploadEvent($user_id, ['file' => $file]);
             if ($app->dispatch("user_upload_{$type}", $event)) {
                 if ($upload = $event->getURL()) {
                     HttpResponse::getInstance()->display(['url' => $upload], '', true);
                 }
             }
         } finally {
             @unlink($file);
         }
     }
     HttpResponse::getInstance()->displayError("Unable to upload file");
 }
Пример #2
0
 /**
  * @param $urls
  *
  * @return array|null
  */
 public function proxy($urls)
 {
     if (!empty($urls)) {
         foreach ($urls as $url) {
             $tmp = PathUtils::getTmpDir('tmp/proxy');
             $path = sprintf("%s/%s.%s", $tmp, md5($url), pathinfo($url, PATHINFO_EXTENSION));
             if ($file = file_exists($path) ? $path : HttpUtils::downloadFile($url, $path)) {
                 $files[] = ['remote' => $url, 'local' => $file];
             }
         }
     }
     return !empty($files) ? $files : null;
 }