/** * Once the file has been uploaded to S3, the CMS will callback this action * and pass along details about the file that we'll use to create an S3File * DataObject. * * Will respond with an some JSON data about the new S3File DataObject so it * can be added to the Form to which our S3FileUploadField is attached. * * Most of this has been adapted from the uplaod action of the UploadField. * @param SS_HTTPRequest $request * @return SS_HTTPResponse */ public function upload(SS_HTTPRequest $request) { if ($this->isDisabled() || $this->isReadonly() || !$this->canUpload()) { return $this->httpError(403); } // Protect against CSRF on destructive action $token = $this->getForm()->getSecurityToken(); if (!$token->checkRequest($request)) { return $this->httpError(400); } // Get form details $postVars = $request->postVars(); $postVars['LastModified'] = date("Y-m-d H:i:s", $postVars['LastModified']); $postVars['ETag'] = str_replace('"', '', $postVars['ETag']); $postVars['Region'] = $this->getRegion(); // Create our S3File $s3File = new S3File($postVars); $s3File->write(); $s3File->customise(array('UploadFieldDeleteLink' => $this->getItemHandler($s3File->ID)->DeleteLink())); // Format response with json $response = new SS_HTTPResponse(Convert::raw2json(array(array('bucket' => $s3File->Bucket, 'etag' => $s3File->ETag, 'id' => $s3File->ID, 'key' => $s3File->Key, 'last_modified' => $s3File->LastModified, 'location' => $s3File->Location, 'name' => $s3File->Name, 'size' => $s3File->Size, 'type' => $s3File->Type, 'fieldname' => $this->getName(), 'buttons' => (string) $s3File->renderWith($this->getTemplateFileButtons()), 'edit_url' => $this->getItemHandler($s3File->ID)->EditLink(), 'thumbnail_url' => $s3File->Icon())))); $response->addHeader('Content-Type', 'application/json'); if (!empty($return['error'])) { $response->setStatusCode(403); } return $response; }
protected function onBeforeDelete() { parent::onBeforeDelete(); $this->deleteFormattedImages(); }
/** * Turn off automatic unique id generation. Unique IDs are used to quell name * collisions. */ public static function disable_unique_ids() { self::$unique_id = false; }
private function _createS3File() { //format the name and stuff $filename = basename($this->args('key')); $filename = str_replace(" ", "_", $filename); $filename = preg_replace("/[^-_.+[0-9a-zA-Z]/", "", $filename); $path = "assets/" . S3File::getNiceDir($filename); //check our info out. $info = $this->_lookupFileInfo(); //create new s3 file $file = new S3File(); $file->set('user_id', User::$me->id); $file->set('type', $info['type']); $file->set('size', $info['size']); $file->set('hash', $info['hash']); $file->set('add_date', date('Y-m-d H:i:s')); $file->set('bucket', AMAZON_S3_BUCKET_NAME); $file->set('path', $path); $file->save(); //copy to new location in s3. $s3 = new S3(AMAZON_AWS_KEY, AMAZON_AWS_SECRET); $s3->copyObject($this->args('bucket'), $this->args('key'), AMAZON_S3_BUCKET_NAME, $path, S3::ACL_PUBLIC_READ); //remove the uploaded file. $s3->deleteObject($this->args('bucket'), $this->args('key')); return $file; }
} if (!defined("AMAZON_AWS_SECRET")) { die("Please define the AMAZON_AWS_SECRET in your config\n"); } if (!defined("STORAGE_METHOD") || STORAGE_METHOD !== "LocalFile") { die("Please define STORAGE_METHOD to be LocalFile\n"); } $total_s3_files = db()->getValue("SELECT max(id) FROM s3_files"); print "Files to convert: " . $total_s3_files . "\n"; $rows_to_remove = array(); for ($s3_id = 1; $s3_id <= $total_s3_files; $s3_id++) { // Grab the s3 file // Verify the file exists. // If it exists, download it // If it doesn't, add it to the rows to be removed // Set all of the correct variables $s3_file = new S3File($s3_id); if ($s3_file->exists()) { $local_file = new LocalFile($s3_id); $temp_file = tempnam("/tmp", "BQ"); $s3_file->download($s3_file->get('path'), $temp_file); $local_file->upload($temp_file, $s3_file->get('path')); $local_file->id = $s3_id; $percent_completed = sprintf("%01.2f", $s3_id * 100 / $total_s3_files); print sprintf("%6s", $percent_completed) . "% => File #" . $s3_id . "\n"; } else { print "File #" . $s3_id . " doesn't exist\n"; $rows_to_remove[] = $s3_id; } } print count($rows_to_remove) . " files were no longer in S3\n";
public function create() { $this->assertLoggedIn(); if ($this->args('step2')) { $this->setTitle('Step 2 of 2: Create Job'); } else { $this->setTitle('Create new Job'); } try { if ($this->args('job_id')) { $job = new Job($this->args('job_id')); if (!$job->isHydrated()) { throw new Exception("That job does not exist."); } if ($job->get('user_id') != User::$me->id) { throw new Exception("You do not own this job."); } $file = $job->getFile(); $queue_id = $job->get('queue_id'); } else { $file = new S3File($this->args('file_id')); } if (!$file->isHydrated()) { throw new Exception("That file does not exist."); } if ($file->get('user_id') != User::$me->id) { throw new Exception("You do not have access to this file."); } $this->set('file', $file); //load up our form. $form = $this->_createJobForm($file, $queue_id); if (isset($job)) { $form->action = "/job/create/job:{$job->id}"; } else { $form->action = "/job/create/file:{$file->id}"; } //handle our form if ($form->checkSubmitAndValidate($this->args())) { //pull in our quantity $quantity = (int) $form->data('quantity'); $quantity = max(1, $quantity); $quantity = min(1000, $quantity); //queue error checking. $queue = new Queue($form->data('queue_id')); if (!$queue->isHydrated()) { throw new Exception("That queue does not exist."); } if (!$queue->canAdd()) { throw new Exception("You do not have permission to add to that queue."); } //okay, we good? $queue->addGCodeFile($file, $quantity); Activity::log("added {$quantity} new " . Utility::pluralizeWord('job', $quantity)); $this->forwardToUrl($queue->getUrl()); } $this->set('form', $form); } catch (Exception $e) { $this->set('megaerror', $e->getMessage()); } }