Renamed path to file_path in File properties

This commit is contained in:
2015-11-05 18:47:36 +01:00
parent 4e5d076f49
commit a661c1c107
6 changed files with 62 additions and 17 deletions

View File

@@ -349,6 +349,7 @@ def post_GET_user(request, payload):
app.on_post_GET_users += post_GET_user app.on_post_GET_users += post_GET_user
from modules.file_storage import process_file from modules.file_storage import process_file
from modules.file_storage import delete_file
def post_POST_files(request, payload): def post_POST_files(request, payload):
"""After an file object has been created, we do the necessary processing """After an file object has been created, we do the necessary processing
@@ -362,15 +363,15 @@ from utils.cdn import hash_file_path
from application.utils.gcs import GoogleCloudStorageBucket from application.utils.gcs import GoogleCloudStorageBucket
# Hook to check the backend of a file resource, to build an appropriate link # Hook to check the backend of a file resource, to build an appropriate link
# that can be used by the client to retrieve the actual file. # that can be used by the client to retrieve the actual file.
def generate_link(backend, path, project_id=None): def generate_link(backend, file_path, project_id=None):
if backend == 'gcs': if backend == 'gcs':
storage = GoogleCloudStorageBucket(project_id) storage = GoogleCloudStorageBucket(project_id)
blob = storage.Get(path) blob = storage.Get(file_path)
link = blob['signed_url'] link = blob['signed_url']
elif backend == 'pillar': elif backend == 'pillar':
link = url_for('file_storage.index', file_name=path, _external=True) link = url_for('file_storage.index', file_name=file_path, _external=True)
elif backend == 'cdnsun': elif backend == 'cdnsun':
link = hash_file_path(path, None) link = hash_file_path(file_path, None)
else: else:
link = None link = None
return link return link
@@ -378,18 +379,24 @@ def generate_link(backend, path, project_id=None):
def before_returning_file(response): def before_returning_file(response):
# TODO: add project id to all files # TODO: add project id to all files
project_id = None if 'project' not in response else str(response['project']) project_id = None if 'project' not in response else str(response['project'])
response['link'] = generate_link(response['backend'], response['path'], project_id) response['link'] = generate_link(response['backend'], response['file_path'], project_id)
def before_returning_files(response): def before_returning_files(response):
for item in response['_items']: for item in response['_items']:
# TODO: add project id to all files # TODO: add project id to all files
project_id = None if 'project' not in item else str(item['project']) project_id = None if 'project' not in item else str(item['project'])
item['link'] = generate_link(item['backend'], item['path'], project_id) item['link'] = generate_link(item['backend'], item['file_path'], project_id)
app.on_fetched_item_files += before_returning_file app.on_fetched_item_files += before_returning_file
app.on_fetched_resource_files += before_returning_files app.on_fetched_resource_files += before_returning_files
def before_deleting_file(item):
delete_file(item)
app.on_delete_item_files += before_deleting_file
# The file_storage module needs app to be defined # The file_storage module needs app to be defined
from modules.file_storage import file_storage from modules.file_storage import file_storage
#from modules.file_storage.serve import * #from modules.file_storage.serve import *

View File

@@ -51,7 +51,7 @@ def build_thumbnails(file_path=None, file_id=None):
files_collection = app.data.driver.db['files'] files_collection = app.data.driver.db['files']
if file_path: if file_path:
# Search file with backend "pillar" and path=file_path # Search file with backend "pillar" and path=file_path
file_ = files_collection.find({"path": "{0}".format(file_path)}) file_ = files_collection.find({"file_path": "{0}".format(file_path)})
file_ = file_[0] file_ = file_[0]
if file_id: if file_id:
@@ -72,7 +72,7 @@ def build_thumbnails(file_path=None, file_id=None):
if thumbnail.get('exists'): if thumbnail.get('exists'):
# If a thumbnail was already made, we just continue # If a thumbnail was already made, we just continue
continue continue
basename = os.path.basename(thumbnail['path']) basename = os.path.basename(thumbnail['file_path'])
root, ext = os.path.splitext(basename) root, ext = os.path.splitext(basename)
file_object = dict( file_object = dict(
name=root, name=root,
@@ -88,7 +88,7 @@ def build_thumbnails(file_path=None, file_id=None):
md5=thumbnail['md5'], md5=thumbnail['md5'],
filename=basename, filename=basename,
backend=file_['backend'], backend=file_['backend'],
path=basename, file_path=basename,
project=file_['project']) project=file_['project'])
# Commit to database # Commit to database
r = post_item('files', file_object) r = post_item('files', file_object)
@@ -131,7 +131,7 @@ def process_file(src_file):
content_type = src_file['content_type'].split('/') content_type = src_file['content_type'].split('/')
src_file['format'] = content_type[1] src_file['format'] = content_type[1]
mime_type = content_type[0] mime_type = content_type[0]
src_file['path'] = src_file['name'] src_file['file_path'] = src_file['name']
if mime_type == 'image': if mime_type == 'image':
from PIL import Image from PIL import Image
@@ -183,7 +183,7 @@ def process_file(src_file):
md5="", # Available after encode md5="", # Available after encode
filename=os.path.split(filename)[1], filename=os.path.split(filename)[1],
backend=src_file['backend'], backend=src_file['backend'],
path=filename) file_path=filename)
file_object_id = files_collection.save(file_object) file_object_id = files_collection.save(file_object)
# Append the ObjectId to the new list # Append the ObjectId to the new list
@@ -226,3 +226,27 @@ def process_file(src_file):
file_asset = files_collection.find_and_modify( file_asset = files_collection.find_and_modify(
{'_id': src_file['_id']}, {'_id': src_file['_id']},
src_file) src_file)
def delete_file(file_item):
def process_file_delete(file_item):
"""Given a file item, delete the actual file from the storage backend.
This function can be probably made self-calling."""
if file_item['backend'] == 'gcs':
storage = GoogleCloudStorageBucket(str(file_item['project']))
storage.Delete(file_item['file_path'])
return True
elif backend == 'pillar':
pass
elif backend == 'cdnsun':
pass
else:
pass
files_collection = app.data.driver.db['files']
# Collect children (variations) of the original file
children = files_collection.find({'parent': file_item['_id']})
for child in children:
process_file_delete(child)
# Finally remove the original file
process_file_delete(file_item)

View File

@@ -2,6 +2,7 @@ import os
import time import time
import datetime import datetime
from gcloud.storage.client import Client from gcloud.storage.client import Client
from gcloud.exceptions import NotFound
from oauth2client.client import SignedJwtAssertionCredentials from oauth2client.client import SignedJwtAssertionCredentials
from application import app from application import app
@@ -101,7 +102,7 @@ class GoogleCloudStorageBucket(object):
signed_url=blob.generate_signed_url(expiration, credentials=self.credentials_p12)) signed_url=blob.generate_signed_url(expiration, credentials=self.credentials_p12))
def Get(self, path): def Get(self, path, to_dict=True):
"""Get selected file info if the path matches. """Get selected file info if the path matches.
:type path: string :type path: string
@@ -110,7 +111,10 @@ class GoogleCloudStorageBucket(object):
path = os.path.join(self.subdir, path) path = os.path.join(self.subdir, path)
blob = self.bucket.blob(path) blob = self.bucket.blob(path)
if blob.exists(): if blob.exists():
return self.blob_to_dict(blob) if to_dict:
return self.blob_to_dict(blob)
else:
return blob
else: else:
return None return None
@@ -125,3 +129,15 @@ class GoogleCloudStorageBucket(object):
blob.upload_from_filename(full_path) blob.upload_from_filename(full_path)
return blob return blob
# return self.blob_to_dict(blob) # Has issues with threading # return self.blob_to_dict(blob) # Has issues with threading
def Delete(self, path):
"""Delete blob (when removing an asset or replacing a preview)"""
# We want to get the actual blob to delete
blob = self.Get(path, to_dict=False)
try:
blob.delete()
return True
except NotFound:
return None

View File

@@ -43,7 +43,7 @@ def generate_local_thumbnails(src, return_image_stats=False):
format = im.format.lower() format = im.format.lower()
# Get format # Get format
thumbnails[size] = dict( thumbnails[size] = dict(
path=dst, # Full path, to be processed before storage file_path=dst, # Full path, to be processed before storage
length=length, length=length,
width=width, width=width,
height=height, height=height,

View File

@@ -623,7 +623,6 @@ def populate_node_types(old_ids={}):
'methods': ['GET', 'PUT', 'POST'] 'methods': ['GET', 'PUT', 'POST']
}], }],
'users': [], 'users': [],
'world': ['GET']
} }
} }
@@ -664,7 +663,6 @@ def populate_node_types(old_ids={}):
'methods': ['GET', 'PUT', 'POST'] 'methods': ['GET', 'PUT', 'POST']
}], }],
'users': [], 'users': [],
'world': ['GET']
} }
} }

View File

@@ -392,7 +392,7 @@ files_schema = {
'required': True, 'required': True,
'allowed': ["attract-web", "pillar", "cdnsun", "gcs"] 'allowed': ["attract-web", "pillar", "cdnsun", "gcs"]
}, },
'path': { 'file_path': {
'type': 'string', 'type': 'string',
#'required': True, #'required': True,
'unique': True, 'unique': True,