public function persist(ImageUpload $imageupload) { $s3 = new S3Service($this->config); $s3Name = $s3->persistImageUpload($imageupload); $sns = new SNSService($this->config); $sns->sendNotification($imageupload, $s3Name); }
public function fetchLatestGrams() { $db = new DynamoDBService($this->config); $raw = $db->getLatestGrams(); $s3 = new S3Service($this->config); $grams = array(); foreach ($raw as $protogram) { $ref = current($protogram['gramified']); $grams[$ref]['uploadDate'] = current($protogram['uploadDate']); $grams[$ref]['message'] = current($protogram['message']); $grams[$ref]['gramified'] = $s3->getGramified($ref); } return $grams; }
public function handleMessage(array $message) { if ($message['Type'] == 'SubscriptionConfirmation') { $sns = new SNSService($this->config); $sns->confirmSubscription($message); } elseif ($message['Type'] == 'Notification') { $s3 = new S3Service($this->config); $url = $s3->getImageUrl($message['Subject']); $img = $this->resizeImage($url); $text = $img ? $this->gramifyImage($img) : 'Error!'; $textName = $s3->persistGramified($text); $db = new DynamoDBService($this->config); $db->persist($message['Subject'], $textName, $message['Message']); } }
private function createAndUploadImageDimensionMap(Image $image, $dimensionsKeyname) { $imd = $this->getDimensionByKeyname($dimensionsKeyname); $srcPth = $this->imagesRoot . $image->FilePath; if (file_exists($srcPth)) { $tempPath = tempnam(sys_get_temp_dir(), 'img_'); if ($dimensionsKeyname == 'original') { $s3Path = $srcPth; $pts = getimagesize($s3Path); $width = $pts[0]; $height = $pts[1]; $newDims = array('x' => $width, 'y' => $height); $s3Path = $srcPth; } else { //need to redimension this image, using MagickWand $magick_wand = NewMagickWand(); MagickReadImage($magick_wand, $srcPth); $width = MagickGetImageWidth($magick_wand); $height = MagickGetImageHeight($magick_wand); $newDims = $this->getNewDimensionsPreservingAspectRatio($width, $height, $imd->Width, $imd->Height); MagickScaleImage($magick_wand, $newDims['x'], $newDims['y']); MagickWriteImage($magick_wand, $tempPath); $s3Path = $tempPath; } $headers = array(); //This holds the http headers for our S3 object. switch ($image->ImageType) { case 'GIF': $headers['Content-Type'] = 'image/gif'; break; case 'JPG': $headers['Content-Type'] = 'image/jpeg'; break; case 'PNG': $headers['Content-Type'] = 'image/png'; break; default: throw new Exception("Unrecognized type {$image->getType()}"); break; } //A "Far Future" expiration - maximizing the chance that the user's web browser will use //a cached version rather than requesting the file from Cloudfront. //Also set to public (as recommended by Google Speed Tracer), so that caching will work when //using SSL as well. Even though Cloudfont doesn't support ssl today, someday it will //and we will be prepared! $headers['Cache-Control'] = 'public, max-age=315360000'; //10 years $imDimMap = new ImageDimensionsMap(); $imDimMap->ImageId = $image->Id; $imDimMap->ImageDimensionsId = $imd->Id; $imDimMap->Width = $newDims['x']; $imDimMap->Height = $newDims['y']; $imDimMap->Version = $image->Version; $imDimMap->save(); //upload the new file to S3 try { $acl = S3Service::ACL_PUBLIC_READ; if (!file_exists($tempPath)) { throw new Exception("{$tempPath} dosn't exist"); } $res = $this->s3->putObject(S3Service::inputFile($s3Path, true), $this->bucket, $this->getPath($image, $imDimMap, $imd), $acl, array(), $headers); if ($res) { unlink($tempPath); } else { //something's wrong. Fail silently and just leave the old version if it exists. //Don't throw an exception or raise a failure, because there's a user on the other side //of this request waiting to see this image. $imDimMap->Version = $imDimMap->Version - 1; $imDimMap->save(); } } catch (Exception $e) { //something's wrong. Fail silently and just leave the old version if it exist. //Don't throw an exception or raise a failure, because there's a user on the other side //of this request waiting to see this image. $imDimMap->Version = $imDimMap->Version - 1; $imDimMap->save(); } } return $imDimMap; }
/** * Get the S3 response * * @return object | false */ public function getResponse() { $query = ''; if (sizeof($this->parameters) > 0) { $query = substr($this->uri, -1) !== '?' ? '?' : '&'; foreach ($this->parameters as $var => $value) { if ($value == null || $value == '') { $query .= $var . '&'; } else { $query .= $var . '=' . rawurlencode($value) . '&'; } } $query = substr($query, 0, -1); $this->uri .= $query; if (array_key_exists('acl', $this->parameters) || array_key_exists('location', $this->parameters) || array_key_exists('torrent', $this->parameters) || array_key_exists('logging', $this->parameters)) { $this->resource .= $query; } } $url = (S3Service::$useSSL && extension_loaded('openssl') ? 'https://' : 'http://') . $this->headers['Host'] . $this->uri; //var_dump($this->bucket, $this->uri, $this->resource, $url); // Basic setup $curl = curl_init(); curl_setopt($curl, CURLOPT_USERAGENT, 'S3/php'); if (S3Service::$useSSL) { curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1); } curl_setopt($curl, CURLOPT_URL, $url); // Headers $headers = array(); $amz = array(); foreach ($this->amzHeaders as $header => $value) { if (strlen($value) > 0) { $headers[] = $header . ': ' . $value; } } foreach ($this->headers as $header => $value) { if (strlen($value) > 0) { $headers[] = $header . ': ' . $value; } } // Collect AMZ headers for signature foreach ($this->amzHeaders as $header => $value) { if (strlen($value) > 0) { $amz[] = strtolower($header) . ':' . $value; } } // AMZ headers must be sorted if (sizeof($amz) > 0) { sort($amz); $amz = "\n" . implode("\n", $amz); } else { $amz = ''; } // Authorization string (CloudFront stringToSign should only contain a date) $headers[] = 'Authorization: ' . S3Service::__getSignature($this->headers['Host'] == 'cloudfront.amazonaws.com' ? $this->headers['Date'] : $this->verb . "\n" . $this->headers['Content-MD5'] . "\n" . $this->headers['Content-Type'] . "\n" . $this->headers['Date'] . $amz . "\n" . $this->resource); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback')); curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this, '__responseHeaderCallback')); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Request types switch ($this->verb) { case 'GET': break; case 'PUT': case 'POST': // POST only used for CloudFront if ($this->fp !== false) { curl_setopt($curl, CURLOPT_PUT, true); curl_setopt($curl, CURLOPT_INFILE, $this->fp); if ($this->size >= 0) { curl_setopt($curl, CURLOPT_INFILESIZE, $this->size); } } elseif ($this->data !== false) { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb); curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data); if ($this->size >= 0) { curl_setopt($curl, CURLOPT_BUFFERSIZE, $this->size); } } else { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb); } break; case 'HEAD': curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD'); curl_setopt($curl, CURLOPT_NOBODY, true); break; case 'DELETE': curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); break; default: break; } // Execute, grab errors if (curl_exec($curl)) { $this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE); } else { $this->response->error = array('code' => curl_errno($curl), 'message' => curl_error($curl), 'resource' => $this->resource); } @curl_close($curl); // Parse body into XML if ($this->response->error === false && isset($this->response->headers['type']) && $this->response->headers['type'] == 'application/xml' && isset($this->response->body)) { $this->response->body = simplexml_load_string($this->response->body); // Grab S3 errors if (!in_array($this->response->code, array(200, 204)) && isset($this->response->body->Code, $this->response->body->Message)) { $this->response->error = array('code' => (string) $this->response->body->Code, 'message' => (string) $this->response->body->Message); if (isset($this->response->body->Resource)) { $this->response->error['resource'] = (string) $this->response->body->Resource; } unset($this->response->body); } } // Clean up file resources if ($this->fp !== false && is_resource($this->fp)) { fclose($this->fp); } return $this->response; }