/** * Get a resized image URL for an attachment image * * Uses Google Cloud Storage to resize and serve an attachment image. * * @wp-filter image_downsize * * @param null|array $data Existing data (we always override) * @param int $id Attachment ID * @param string $size Size ID * @return array Indexed array of URL, width, height, is intermediate */ public static function get_intermediate_url($data, $id, $size) { $file = get_attached_file($id); if (0 !== strpos($file, 'gs://') || self::$skip_image_filters) { return $data; } $sizes = self::image_sizes(); if (is_array($size)) { $size = ['width' => $size[0], 'height' => $size[1], 'crop' => false]; } else { $size = $sizes[$size]; } $options = []; // If height or width is null (i.e. full size), $real_size will be // null, providing us a way to tell if the size is intermediate $real_size = max($size['height'], $size['width']); if ($real_size) { $options = ['size' => $real_size, 'crop' => (bool) $size['crop']]; } else { $options = ['size' => 0, 'crop' => false]; } $baseurl = get_post_meta($id, '_appengine_imageurl', true); $cached_file = get_post_meta($id, '_appengine_imageurl_file', true); $secure_urls = (bool) get_option(self::USE_SECURE_URLS_OPTION, false); if (empty($baseurl) || $cached_file !== $file) { try { if (self::is_production()) { $options = ['secure_url' => $secure_urls]; $baseurl = CloudStorageTools::getImageServingUrl($file, $options); } else { $baseurl = CloudStorageTools::getPublicUrl($file, $secure_urls); } update_post_meta($id, '_appengine_imageurl', $baseurl); update_post_meta($id, '_appengine_imageurl_file', $file); } catch (CloudStorageException $e) { syslog(LOG_ERR, 'There was an exception creating the Image Serving URL, details ' . $e->getMessage()); self::$skip_image_filters = true; $data = image_downsize($id, $size); self::$skip_image_filters = false; return $data; } } $url = $baseurl; // Only append image options to the URL if we're running in production, // since in the development context getPublicUrl() is currently used to // generate the URL. if (self::is_production()) { if (!is_null($options['size'])) { $url .= '=s' . $options['size']; if ($options['crop']) { $url .= '-c'; } } else { $url .= '=s0'; } } $data = [$url, $size['width'], $size['height'], (bool) $real_size]; return $data; }
use google\appengine\api\cloud_storage\CloudStorageTools; $temp = explode(".", $thatfile); $ext = end($temp); if (in_array($ext, $valid_formats)) { if ($var6 < 200000) { echo "fil size is "; echo $var6; $fileName = 'gs://venkat123/' . $_FILES['uploaded_files']['name']; // echo $fileName."<br>"; $options = array('gs' => array('acl' => 'public-read', 'Content-Type' => $_FILES['uploaded_files']['type'])); $ctx = stream_context_create($options); if (false == rename($_FILES['uploaded_files']['tmp_name'], $fileName, $ctx)) { die('Could not rename.'); } // $var2 =(new \ DateTime())->format('i:s'); $object_public_url = CloudStorageTools::getPublicUrl($fileName, true); $var1 = date("Y-m-d H:i:s"); $var2 = microtime(true); $var3 = $var2 - $var1; // echo $var3; // echo $object_public_url."<br>"; // print $thisfile; // print $thatfile; // var_dump($_FILES); $db = new pdo('mysql:unix_socket=/cloudsql/ordinal-gear-93506:testdb;dbname=g1', 'root', ''); $stmt1 = $db->prepare("select * from another where filename='{$thatfile}'"); $stmt1->execute(); $row = $stmt1->fetch(); if ($row > 0) { echo "duplicate entry"; // $db->exec("insert into users values('8975','200')");
/** * Create a URL for a target bucket and optional object. * * @visibleForTesting */ public static function createObjectUrl($bucket, $object = null) { // Strip leading "/" for $object if (isset($object) && $object[0] == "/") { $object_name = substr($object, 1); } else { $object_name = ""; } $gs_filename = CloudStorageTools::getFilename($bucket, $object_name); return CloudStorageTools::getPublicUrl($gs_filename, true); }
function message_create($user_id_sender, $user_id_target) { // upload_file /* * Adds record to table MESSAGE * Adds file metadata into table MESSAGE * Adds a record to table MESSAGE_DISTO for each target (recipient) // DEV_NOTE: currently only supports one recipient * * URL: /server?action=message_create */ // get file meta data to insert into database and for response body $file_name = $_FILES['userfile']['name']; $file_type = $_FILES['userfile']['type']; $file_size = $_FILES['userfile']['size']; // get current timestamp and timezone $date = date_create(); //$tz = $date -> getTimezone(); $timestamp = date_format($date, 'Y-m-d H:i:s'); //$timezone = $tz -> getName(); // get error and temp info //$file_tmp_name = $_FILES['userfile']['tmp_name']; $file_error = $_FILES['userfile']['error']; $gs_name = $_FILES['userfile']['tmp_name']; // create user directory if it does not exist if (!file_exists('gs://androidsoundappproject.appspot.com/message/' . $user_id_sender)) { // create dir mkdir('gs://androidsoundappproject.appspot.com/message/' . $user_id_sender); } // save file $object_url = 'gs://androidsoundappproject.appspot.com/message/' . $user_id_sender . '/' . $file_name; //$options = stream_context_create(['gs'=>['acl'=>'public-read']]); $file = file_get_contents($gs_name); $options = array('gs' => array('acl' => 'public-read', 'Content-Type' => $_FILES['userfile']['type'])); $ctx = stream_context_create($options); // place file into storage and give it public access if (true == file_put_contents($object_url, $file, 0, $ctx)) { //if (true == rename($_FILES['userfile']['tmp_name'], $object_url, $ctx)) { //if (move_uploaded_file($gs_name, $object_url)) { // may need to rename file with unique name // upload success $status = 201; header('Created', true, $status); $file_path = $user_id_sender . '/' . $file_name; $file_public_url = CloudStorageTools::getPublicUrl($object_url, false); // add file metadata to database table MESSAGE $dbh = dbConn(); $sql = 'INSERT INTO MESSAGE (USER_ID_SENDER, FILE_NAME, FILE_TYPE, FILE_SIZE, FILE_PATH, DATE_SENT) VALUES (:USER_ID_SENDER, :FILE_NAME, :FILE_TYPE, :FILE_SIZE, :FILE_PATH, :DATE_SENT)'; $qry = $dbh->prepare($sql); // parameter array $file_meta = array(':USER_ID_SENDER' => $user_id_sender, ':FILE_NAME' => $file_name, ':FILE_TYPE' => $file_type, ':FILE_SIZE' => $file_size, ':FILE_PATH' => $file_path, ':DATE_SENT' => $timestamp); $qry->execute($file_meta); $msg_id = $dbh->lastInsertId(); // add recipients to database table MESSAGE_DISTRO // DEV_NOTE: add loop if we allow multiple recipients $sql = 'INSERT INTO MESSAGE_DISTRO (MSG_ID, USER_ID_SENDER, USER_ID_TARGET) VALUES (:MSG_ID, :USER_ID_SENDER, :USER_ID_TARGET)'; $qry = $dbh->prepare($sql); // parameter array $distro = array(':MSG_ID' => $msg_id, ':USER_ID_SENDER' => $user_id_sender, ':USER_ID_TARGET' => $user_id_target); $qry->execute($distro); dbClose($dbh); } else { // upload fail $status = 501; header('Method Not Implemented', true, $status); $msg_id = null; //die('Could not rename.'); } // build response array $response = array('MSG_ID' => $msg_id, 'USER_ID_SENDER' => $user_id_sender, 'FILE_NAME' => $file_name, 'FILE_TYPE' => $file_type, 'FILE_SIZE' => $file_size, 'FILE_PATH' => $file_path, 'FILE_PUBLIC_URL' => $file_public_url, 'DATE_SENT' => $timestamp, 'USER_ID_TARGET' => $user_id_target, 'tmp_name' => $gs_name, 'status' => $status, 'error' => $file_error); return $response; //return(sendResponse($response)); }
public function testGetPublicUrlEncoding() { $bucket = "bucket"; $object = " %#?"; $gs_filename = sprintf("gs://%s/%s", $bucket, $object); $expected = "https://bucket.storage.googleapis.com/%20%25%23%3F"; $actual = CloudStorageTools::getPublicUrl($gs_filename, true); $this->assertEquals($expected, $actual); }
/** * Create a URL for a target bucket and optional object. * * @visibleForTesting */ public static function createObjectUrl($bucket, $object = null) { $gs_filename = self::createGcsFilename($bucket, $object); return CloudStorageTools::getPublicUrl($gs_filename, true); }
/** * Create a URL for a target bucket and optional object. * * @visibleForTesting */ public static function createObjectUrl($bucket, $object = null) { if (!isset($object)) { $object = ""; } // Strip leading "/" for $object. if (StringUtil::startsWith($object, "/")) { $object = substr($object, 1); } $gs_filename = CloudStorageTools::getFilename($bucket, $object); return CloudStorageTools::getPublicUrl($gs_filename, true); }