Пример #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
 public function upload(UploadEvent $event)
 {
     set_time_limit(max(100, ini_get('max_execution_time')));
     if (!$event->getUrl()) {
         #some other uploader has already taken care of it?
         if ($data = $event->getEventData()) {
             if ($file = @$data['file']) {
                 if (file_exists($file)) {
                     $app = App::getInstance();
                     $user_id = $event->getUserId();
                     if ($s3conf = $app->config->getKey('private/api_keys/aws-s3')) {
                         if (!empty($s3conf['access_key']) && !empty($s3conf['secret_key']) && !empty($s3conf['bucket_name'])) {
                             if ($user_id > 0 || !empty($s3conf['anonymous_uploads'])) {
                                 try {
                                     $s3 = S3Client::factory(array('key' => $s3conf['access_key'], 'secret' => $s3conf['secret_key'], 'region' => @$s3conf['region'] ?: Region::US_EAST_1));
                                     $type = PathUtils::getFileType($file);
                                     $file_key = sprintf("users/%s/%s/%d-%s", $user_id > 0 ? $user_id : 'anon', $type, filesize($file), basename($file));
                                     $result = $s3->putObject(array('Bucket' => $s3conf['bucket_name'], 'SourceFile' => $file, 'Key' => $file_key, 'ContentType' => 'text/plain', 'ACL' => 'public-read', 'StorageClass' => 'REDUCED_REDUNDANCY'));
                                     if (!empty($result['ObjectURL'])) {
                                         $url = $result['ObjectURL'];
                                         if (!empty($s3conf['cdn'])) {
                                             $event->setUrl(sprintf('http://%s/%s', $s3conf['cdn'], $file_key));
                                         } else {
                                             $event->setUrl($url);
                                         }
                                     } else {
                                         throw new UploadError("Error uploading to S3: No url returned");
                                     }
                                 } catch (Exception $e) {
                                     throw new UploadError("Error uploading to S3: " . $e->getMessage(), $e);
                                 }
                             } else {
                                 throw new LoginError("You must be logged in to upload.", $user_id);
                             }
                         } else {
                             throw new UploadError("S3 configuration is incomplete.", $s3conf);
                         }
                     } else {
                         throw new UploadError("S3 configuration is empty");
                     }
                 } else {
                     throw new UploadError("Upload file is missing");
                 }
             } else {
                 throw new UploadError("Upload data is empty");
             }
         }
     }
 }