def upload_watermarked_image(image, job_id, new_url):
	# gets the information of the new url
	filename, content_type, extension = get_file_info(new_url)

	gcs_filename = 'watermarked/%s/%s' % (job_id, filename)
	watermarked_file = '/%s/%s' % (BUCKET_NAME, gcs_filename)
	new_url = PHOTOS_ROOT_URL + '/' + gcs_filename

	# render the image as a string
	output = StringIO.StringIO()
	image.save(output, format="jpeg")
	image_output = output.getvalue()
	output.close()

	# upload the file to google cloud storage
	retry_params = gcs.RetryParams(backoff_factor=1.1)
	gcs_file = gcs.open(watermarked_file, 'w', content_type=content_type, options={'x-goog-acl': 'public-read'})
	gcs_file.write(image_output)
	gcs_file.close()

	query_file = '/gs' + watermarked_file
	pprint.pprint('looking for the key: %s' % query_file)
	key = blobstore.create_gs_key(query_file)
	new_url = images.get_serving_url(key)

	pprint.pprint('key:')
	pprint.pprint(key)

	return new_url
def download_image_to_googlecloud(url, job_id):
	filename, content_type, ext = get_file_info(url)

	pprint.pprint('Downloading file: %s' % url)
	pprint.pprint('Filename: %s' % filename)
	pprint.pprint('Content-Type: %s' % content_type)

	result = execute_get(url)

	if result['status_code'] == 200:
		gcs_filename = 'original/%s/%s' % (job_id, filename)
		original_file = '/%s/%s' % (BUCKET_NAME, gcs_filename)
		new_url = PHOTOS_ROOT_URL + '/' + gcs_filename

		retry_params = gcs.RetryParams(backoff_factor=1.1)
		gcs_file = gcs.open(original_file, 'w', content_type=content_type, options={'x-goog-acl': 'public-read'})
		gcs_file.write(result['body'])
		gcs_file.close()

		content = StringIO.StringIO(result['body'])
		image = Image.open(content)

		return new_url, image

	else:
		return None, None

	return new_url
def package_medias():
	# find the job which is being packaged
	job_id = request.form['job_id']
	job = _get_job(job_id)

	pprint.pprint('========= BACKGROUND PROCESSING ')
	if 'photos' in job.medias:
		imz = ioh.InMemoryZip()

		x = 1
		for p in job.medias['photos']:

			img_url = ioh.get_bigfile_url(p['url'])
			filename, content_type, extension = ioh.get_file_info(img_url)
			content = ioh.download_url(img_url)

			pprint.pprint('Url: %s' % img_url)
			pprint.pprint('File Name: %s' % filename)
			pprint.pprint('Has content?: %s' % content is not None)

			img_filename = str(x).zfill(3) + '.jpg'
			imz.append(img_filename, content.getvalue())

			x += 1

		zip_filename = 'packages/%s.zip' % job.key.urlsafe()
		url = imz.upload_to_gcs(BUCKET_NAME, zip_filename)
		url = PHOTOS_ROOT_URL + '/' + zip_filename

		pprint.pprint('==============')
		pprint.pprint('Zip: %s ' % url)
		pprint.pprint('==============')

		job.packaged_photos = url
		job.put()

	else:
		pprint.pprint('No photos to be processed')

	return '', 200