Permalink
Browse files

Merge pull request #30 from Zauberfisch/master

loadUploadedImage() === loadUploaded() AND S3File->exists()
  • Loading branch information...
unclecheese committed Jun 4, 2012
2 parents a4ea7c3 + 762dc8f commit e24ed145c8eafdc8e8522d2f45c4111c0a6a2581
Showing with 47 additions and 74 deletions.
  1. +39 −22 code/s3/S3File.php
  2. +8 −52 code/s3/S3Image.php
View
@@ -60,12 +60,6 @@ class S3File extends DataObject {
protected $uploadBucket = null;
- /**
- * @var string A custom filename
- */
- public $fileName = null;
-
-
/**
* Set the two API keys needed to connect to S3
*
@@ -108,6 +102,16 @@ public function __construct($record = null, $isSingleton = false) {
$this->S3 = new S3(self::$access_key, self::$secret_key);
}
+ /**
+ * A File exists if it has ID and S3 URL
+ * Does not do any filesystem checks.
+ *
+ * @return boolean
+ */
+ public function exists() {
+ return parent::exists() && $this->URL;
+ }
+
/**
* Sets a custom upload bucket for this instance. Overrides {@see self::$default_bucket}
@@ -143,30 +147,25 @@ public function getUploadBucket() {
public function loadUploaded($filedata) {
if(!is_array($filedata) || !isset($filedata['tmp_name']))
return false;
-
$fileTempName = $filedata['tmp_name'];
$fileName = $filedata['name'];
- if(!$this->fileName) {
+ if(!$this->Name) {
$fileName = ereg_replace(' +','-',trim($fileName));
$fileName = ereg_replace('[^A-Za-z0-9.+_\-]','',$fileName);
- if(self::$unique_id) {
- $ext = File::get_file_extension($fileName);
- $base = basename($fileName,".{$ext}");
- $this->Name = uniqid($base).".{$ext}";
- }
+ $ext = File::get_file_extension($fileName);
+ $this->Name = basename($fileName,".$ext");
+ if(self::$unique_id)
+ $this->Name .= uniqid('-');
+ $this->Name .= ".$ext";
}
- else {
- $this->Name = $this->fileName . "." . File::get_file_extension($fileName);
- }
-
$bucket = $this->getUploadBucket();
- $this->S3->putBucket($bucket, S3::ACL_PUBLIC_READ);
-
+ // TODO do a putBucket if it doesn't exist?
+ // $this->S3->putBucket($bucket, S3::ACL_PUBLIC_READ);
if ($this->S3->putObjectFile($fileTempName, $bucket, $this->Name, S3::ACL_PUBLIC_READ)) {
$this->Bucket = $bucket;
$this->URL = "http://{$bucket}.s3.amazonaws.com/{$this->Name}";
+ return true;
}
-
return false;
}
@@ -177,11 +176,29 @@ public function loadUploaded($filedata) {
*
* @return string
*/
- public function Filename() {
+ public function getFilename() {
return basename($this->URL);
}
+
+ /**
+ * Getter for the "Filename" field. This is stored as a field for File, but here
+ * it is done dynamically.
+ *
+ * @deprecated use S3File->getFilename() instead
+ * @return string
+ */
+ public function Filename() {
+ return $this->getFilename();
+ }
-
+
+ /**
+ * Get the URL of the cached S3Image
+ * @return string
+ */
+ public function getURL() {
+ return isset($this->record['URL']) ? $this->record['URL'] : false;
+ }
/**
View
@@ -1,4 +1,5 @@
<?php
+
class S3Image extends S3File {
const ORIENTATION_SQUARE = 0;
const ORIENTATION_PORTRAIT = 1;
@@ -64,18 +65,6 @@ public function defineMethods() {
parent::defineMethods();
}
-
- /**
- * An image exists if it has a filename.
- * Does not do any filesystem checks.
- *
- * @return boolean
- */
- public function exists() {
- if(isset($this->record["Filename"])) {
- return true;
- }
- }
/**
* Return an XHTML img tag for this Image,
@@ -114,33 +103,7 @@ function forTemplate() {
* @return boolean
*/
function loadUploadedImage($filedata) {
- if(!is_array($filedata) || !isset($filedata['tmp_name']))
- return false;
-
- $fileTempName = $filedata['tmp_name'];
- $fileName = $filedata['name'];
- if(!$this->fileName) {
- $fileName = ereg_replace(' +','-',trim($fileName));
- $fileName = ereg_replace('[^A-Za-z0-9.+_\-]','',$fileName);
- if(self::$unique_id) {
- $ext = File::get_file_extension($fileName);
- $base = basename($fileName,".{$ext}");
- $this->Name = uniqid($base).".{$ext}";
- }
- }
- else {
- $this->Name = $this->fileName . "." . File::get_file_extension($fileName);
- }
-
- $bucket = $this->getUploadBucket();
- $this->S3->putBucket($bucket, S3::ACL_PUBLIC_READ);
-
- if ($this->S3->putObjectFile($fileTempName, $bucket, $this->Name, S3::ACL_PUBLIC_READ)) {
- $this->Bucket = $bucket;
- $this->URL = "http://{$bucket}.s3.amazonaws.com/{$this->Name}";
- }
-
- return false;
+ return $this->loadUploaded($filedata);
}
public function SetWidth($width) {
@@ -289,7 +252,8 @@ function generateFormattedImage($format, $arg1 = null, $arg2 = null) {
if($gd){
$bucket = $this->getUploadBucket();
- $this->S3->putBucket($bucket, S3::ACL_PUBLIC_READ);
+ // TODO create bucket if it does not exist
+ // $this->S3->putBucket($bucket, S3::ACL_PUBLIC_READ);
$tempFile = tempnam (getTempFolder(), 's3images').File::get_file_extension ($cacheFile);
$gd->writeTo ($tempFile);
$this->S3->putObjectFile($tempFile, $bucket, '_resampled/'.basename ($cacheFile),
@@ -437,13 +401,13 @@ protected function onBeforeDelete() {
class S3Image_Cached extends S3Image {
/**
* Create a new cached image.
- * @param string $filename The filename of the image.
+ * @param string $url The S3 URL of the image.
* @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods. Singletons
* don't have their defaults set.
*/
- public function __construct($filename = null, $isSingleton = false) {
+ public function __construct($url = null, $isSingleton = false) {
parent::__construct(array(), $isSingleton);
- $this->Filename = $filename;
+ $this->URL = $url;
}
public function getRelativePath() {
@@ -455,15 +419,7 @@ public function requireTable() {
}
- /**
- * Get the URL of the cached S3Image
- * @return string
- */
- public function getURL () {
- return $this->Filename;
- }
-
public function debug() {
- return "S3Image_Cached object for $this->Filename";
+ return "S3Image_Cached object for $this->URL";
}
}

0 comments on commit e24ed14

Please sign in to comment.