forked from pedrogimenez/thumbor-cloud-storage
-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added STORAGE (thumbor_cloud_storage.storages.cloud_storage) #9
Open
JackDavidson
wants to merge
3
commits into
Superbalist:master
Choose a base branch
from
JackDavidson:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
# https://github.com/superbalist/thumbor-cloud-storage | ||
|
||
# Licensed under the MIT license: | ||
# http://www.opensource.org/licenses/mit-license | ||
|
||
import pytz | ||
from datetime import datetime | ||
|
||
from os.path import join | ||
from gcloud import storage | ||
|
||
from thumbor.result_storages import BaseStorage | ||
from thumbor.utils import logger | ||
from thumbor.engines import BaseEngine | ||
|
||
|
||
class Storage(BaseStorage): | ||
PATH_FORMAT_VERSION = 'v2' | ||
bucket = None | ||
|
||
def __init__(self, context, shared_client=True): | ||
BaseStorage.__init__(self, context) | ||
self.shared_client = shared_client | ||
self.bucket = self.get_bucket() | ||
|
||
def get_bucket(self): | ||
parent = self | ||
if self.shared_client: | ||
parent = Storage | ||
if not parent.bucket: | ||
bucket_id = self.context.config.get("STORAGE_CLOUD_STORAGE_BUCKET_ID") | ||
project_id = self.context.config.get("STORAGE_CLOUD_STORAGE_PROJECT_ID") | ||
client = storage.Client(project_id) | ||
parent.bucket = client.get_bucket(bucket_id) | ||
return parent.bucket | ||
|
||
@property | ||
def is_auto_webp(self): | ||
return self.context.config.AUTO_WEBP and self.context.request.accepts_webp | ||
|
||
def put(self, path, bytes): | ||
file_abspath = self.normalize_path(path) | ||
logger.debug("[STORAGE] putting at %s" % file_abspath) | ||
bucket = self.get_bucket() | ||
|
||
blob = bucket.blob(file_abspath) | ||
blob.upload_from_string(bytes) | ||
|
||
max_age = self.context.config.MAX_AGE | ||
blob.cache_control = "public,max-age=%s" % max_age | ||
|
||
if bytes: | ||
try: | ||
mime = BaseEngine.get_mimetype(bytes) | ||
blob.content_type = mime | ||
except: | ||
logger.debug("[STORAGE] Couldn't determine mimetype") | ||
|
||
|
||
blob.patch() | ||
|
||
def put_crypto(self, path): | ||
''' | ||
:param path: ignored. always returns None | ||
:return: None, this is not supported | ||
''' | ||
return | ||
|
||
def get(self, path): | ||
file_abspath = self.normalize_path(path) | ||
logger.debug("[STORAGE] getting from %s" % file_abspath) | ||
|
||
bucket = self.get_bucket() | ||
blob = bucket.get_blob(file_abspath) | ||
if not blob or self.is_expired(blob): | ||
return None | ||
return blob.download_as_string() | ||
|
||
def normalize_path(self, path): | ||
path_segments = [self.context.config.get('STORAGE_CLOUD_STORAGE_ROOT_PATH','thumbor/storage').rstrip('/'), Storage.PATH_FORMAT_VERSION, ] | ||
if self.is_auto_webp: | ||
path_segments.append("webp") | ||
|
||
path_segments.extend([self.partition(path), path.lstrip('/'), ]) | ||
|
||
normalized_path = join(*path_segments).replace('http://', '') | ||
return normalized_path | ||
|
||
def partition(self, path_raw): | ||
path = path_raw.lstrip('/') | ||
return join("".join(path[0:2]), "".join(path[2:4])) | ||
|
||
def is_expired(self, blob): | ||
expire_in_seconds = self.context.config.get('STORAGE_EXPIRATION_SECONDS', None) | ||
|
||
if expire_in_seconds is None or expire_in_seconds == 0: | ||
return False | ||
|
||
timediff = datetime.now(pytz.utc) - blob.updated | ||
return timediff.seconds > expire_in_seconds | ||
|
||
def last_updated(self): | ||
path = self.context.request.url | ||
file_abspath = self.normalize_path(path) | ||
logger.debug("[STORAGE] getting from %s" % file_abspath) | ||
|
||
bucket = self.get_bucket() | ||
blob = bucket.get_blob(file_abspath) | ||
|
||
if not blob or self.is_expired(blob): | ||
logger.debug("[STORAGE] image not found at %s" % file_abspath) | ||
return True | ||
|
||
return blob.updated | ||
|
||
def exists(self, path): | ||
file_abspath = self.normalize_path(path) | ||
logger.debug("[STORAGE] getting from %s" % file_abspath) | ||
|
||
bucket = self.get_bucket() | ||
blob = bucket.get_blob(file_abspath) | ||
return blob and not self.is_expired(blob) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be a backwards incompatible change, won't it? Which could incur a bunch of cache misses and costs to those upgrading.
I think it'd be better to recommend this and leave the result storage where it is, and then move the other storage into a subdirectory or different root directory, e.g.
thumbor-strorage
, with a recommendation to move them into better named directories or sub-directories.