public function replaceTraditional() { $app = \Base::instance(); $files_path = $app->get('TEMP') . "files"; $chunks_path = $app->get('TEMP') . "chunks"; if (!file_exists($chunks_path)) { mkdir($chunks_path, \Base::MODE, true); } if (!file_exists($files_path)) { mkdir($files_path, \Base::MODE, true); } $uploader = new \Fineuploader\Traditional\Handler(); // Specify the list of valid extensions, ex. array("jpeg", "xml", "bmp") $uploader->allowedExtensions = array(); // all files types allowed by default // Specify max file size in bytes. $uploader->sizeLimit = 10 * 1024 * 1024; // default is 10 MiB // Specify the input name set in the javascript. $uploader->inputName = "qqfile"; // matches Fine Uploader's default inputName value by default // If you want to use the chunking/resume feature, specify the folder to temporarily save parts. $uploader->chunksFolder = $chunks_path; $method = $_SERVER["REQUEST_METHOD"]; if ($method == "POST") { header("Content-Type: text/plain"); // Call handleUpload() with the name of the folder, relative to PHP's getcwd() $result = $uploader->handleUpload($files_path); // To return a name used for uploaded file you can use the following line. $result["uploadName"] = $uploader->getUploadName(); $result["originalName"] = $uploader->getName(); // was upload successful? if (!empty($result['success'])) { // OK, we have the file in the tmp folder, let's now fire up the assets model and save it to Mongo $slug = $this->inputfilter->clean($this->app->get('PARAMS.slug'), 'string'); $asset = $this->getModel()->setState('filter.slug', $slug)->getItem(); if (empty($asset->id)) { throw new \Exception('Invalid Asset'); } // The file's location in the File System $filename = $result["uploadName"]; $pathinfo = pathinfo($filename); $buffer = file_get_contents($files_path . "/" . $filename); $originalname = $result["originalName"]; $pathinfo_original = pathinfo($originalname); $values = array('storage' => 'gridfs', 'contentType' => $asset->getMimeType($buffer), 'md5' => md5_file($files_path . "/" . $filename), 'url' => null, "title" => \Dsc\String::toSpaceSeparated($asset->inputfilter()->clean($originalname)), "filename" => $originalname); if (empty($values['title'])) { $values['title'] = $values['md5']; } // save the file $asset = $asset->replace($buffer, $values); $result["asset_id"] = (string) $asset->id; $result["slug"] = $asset->{'slug'}; } echo json_encode($result); } else { header("HTTP/1.0 405 Method Not Allowed"); } }
/** * Creates an asset directly from a URL * and send it directly to S3 * * @param string $url * @param array $options * @throws \Exception */ public static function createFromUrlToS3($url, $options = array()) { $app = \Base::instance(); $s3_options = array('clientPrivateKey' => $app->get('aws.clientPrivateKey'), 'serverPublicKey' => $app->get('aws.serverPublicKey'), 'serverPrivateKey' => $app->get('aws.serverPrivateKey'), 'expectedBucketName' => $app->get('aws.bucketname'), 'expectedMaxSize' => $app->get('aws.maxsize'), 'cors_origin' => $app->get('SCHEME') . "://" . $app->get('HOST') . $app->get('BASE')); if (!class_exists('\\Aws\\S3\\S3Client') || empty($s3_options['clientPrivateKey']) || empty($s3_options['serverPublicKey']) || empty($s3_options['serverPrivateKey']) || empty($s3_options['expectedBucketName']) || empty($s3_options['expectedMaxSize'])) { throw new \Exception('Invalid configuration settings'); } $options = $options + array('width' => 460, 'height' => 308); $request = \Web::instance()->request($url); if (empty($request['body'])) { throw new \Exception('Could not download asset from provided URL'); } $model = new static(); $url_path = parse_url($url, PHP_URL_PATH); $pathinfo = pathinfo($url_path); $filename = $model->inputfilter()->clean($url_path); $buffer = $request['body']; $originalname = str_replace("/", "-", $filename); $thumb = null; if ($thumb_binary_data = $model->getThumb($buffer, null, $options)) { $thumb = new \MongoBinData($thumb_binary_data, 2); } $title = \Dsc\String::toSpaceSeparated($model->inputFilter()->clean($originalname)); $values = array('storage' => 's3', 'contentType' => $model->getMimeType($buffer), 'md5' => md5($filename), 'thumb' => $thumb, 'url' => null, "source_url" => $url, "filename" => $filename, 'title' => $title); $model->bind($values); // these need to happen after the bind $model->slug = $model->generateSlug(); $model->_id = new \MongoId(); /** * Push to S3 */ $bucket = $app->get('aws.bucketname'); $s3 = \Aws\S3\S3Client::factory(array('key' => $app->get('aws.serverPublicKey'), 'secret' => $app->get('aws.serverPrivateKey'))); $key = (string) $model->_id; $res = $s3->putObject(array('Bucket' => $bucket, 'Key' => $key, 'Body' => $buffer, 'ContentType' => $model->contentType)); $s3->waitUntil('ObjectExists', array('Bucket' => $bucket, 'Key' => $key)); if (!$s3->doesObjectExist($bucket, $key)) { throw new \Exception("Upload to Amazon S3 failed"); } $objectInfoValues = $s3->headObject(array('Bucket' => $bucket, 'Key' => $key))->getAll(); /** * END Push to S3 */ $model->url = $s3->getObjectUrl($bucket, $key); $model->s3 = array_merge(array(), (array) $model->s3, array('bucket' => $bucket, 'key' => $key, 'uuid' => (string) $model->_id)) + $objectInfoValues; return $model->save(); }
public function handleTraditional() { $app = \Base::instance(); $files_path = $app->get('TEMP') . "files"; $chunks_path = $app->get('TEMP') . "chunks"; if (!file_exists($chunks_path)) { mkdir($chunks_path, \Base::MODE, true); } if (!file_exists($files_path)) { mkdir($files_path, \Base::MODE, true); } $uploader = new \Fineuploader\Traditional\Handler(); $name = $uploader->getName(); $parts = explode('_', $name); $model_number = $parts[0]; $order = (int) explode('.', $parts[1])[0]; //LOOK FOR A PRODUCT $product = (new \Shop\Models\Products())->setCondition('tracking.model_number', $model_number)->getItem(); if (empty($product->id)) { $result = []; $result['success'] = false; $result['error'] = 'Product not found looking for tracking.model_number = ' . $model_number; $result['preventRetry'] = true; echo json_encode($result); exit; } // Specify the list of valid extensions, ex. array("jpeg", "xml", "bmp") $uploader->allowedExtensions = array(); // all files types allowed by default // Specify max file size in bytes. $uploader->sizeLimit = 10 * 1024 * 1024; // default is 10 MiB // Specify the input name set in the javascript. $uploader->inputName = "qqfile"; // matches Fine Uploader's default inputName value by default // If you want to use the chunking/resume feature, specify the folder to temporarily save parts. $uploader->chunksFolder = $chunks_path; $method = $_SERVER["REQUEST_METHOD"]; if ($method == "POST") { header("Content-Type: text/plain"); // Call handleUpload() with the name of the folder, relative to PHP's getcwd() $result = $uploader->handleUpload($files_path); // To return a name used for uploaded file you can use the following line. $result["uploadName"] = $uploader->getUploadName(); $result["originalName"] = $uploader->getName(); // was upload successful? if (!empty($result['success'])) { // OK, we have the file in the tmp folder, let's now fire up the assets model and save it to Mongo $model = new \Assets\Admin\Models\Assets(); $db = $model->getDb(); $grid = $model->collectionGridFS(); // The file's location in the File System $filename = $result["uploadName"]; $pathinfo = pathinfo($filename); $buffer = file_get_contents($files_path . "/" . $filename); $originalname = $result["originalName"]; $pathinfo_original = pathinfo($originalname); $thumb = null; if ($thumb_binary_data = $model->getThumb($buffer, $pathinfo['extension'])) { $thumb = new \MongoBinData($thumb_binary_data, 2); } $values = array('storage' => 'gridfs', 'contentType' => $model->getMimeType($buffer), 'md5' => md5_file($files_path . "/" . $filename), 'thumb' => $thumb, 'url' => null, "title" => \Dsc\String::toSpaceSeparated($model->inputfilter()->clean($originalname)), "filename" => $originalname); if (empty($values['title'])) { $values['title'] = $values['md5']; } // save the file if ($storedfile = $grid->storeFile($files_path . "/" . $filename, $values)) { $model->load(array('_id' => $storedfile)); $model->bind($values); $model->{'slug'} = $model->generateSlug(); $model->{'type'} = 'shop.assets'; $model->save(); } // $storedfile has newly stored file's Document ID $result["asset_id"] = (string) $storedfile; $result["slug"] = $model->{'slug'}; \Dsc\Queue::task('\\Assets\\Models\\Storage\\CloudFiles::gridfstoCDN', array($result['asset_id'], '/product_images/' . $model_number . '/')); } $product->addImage($model->{'slug'}, $order); echo json_encode($result); } else { header("HTTP/1.0 405 Method Not Allowed"); } }