##// END OF EJS Templates
file-store: save DB entry on upload, and track access times.
marcink -
r3457:4532f88d default
parent child Browse files
Show More
@@ -26,11 +26,12 b' from pyramid.httpexceptions import HTTPF'
26 from rhodecode.apps._base import BaseAppView
26 from rhodecode.apps._base import BaseAppView
27 from rhodecode.apps.file_store import utils
27 from rhodecode.apps.file_store import utils
28 from rhodecode.apps.file_store.exceptions import (
28 from rhodecode.apps.file_store.exceptions import (
29 FileNotAllowedException,FileOverSizeException)
29 FileNotAllowedException, FileOverSizeException)
30
30
31 from rhodecode.lib import helpers as h
31 from rhodecode.lib import helpers as h
32 from rhodecode.lib import audit_logger
32 from rhodecode.lib import audit_logger
33 from rhodecode.lib.auth import (CSRFRequired, NotAnonymous)
33 from rhodecode.lib.auth import (CSRFRequired, NotAnonymous)
34 from rhodecode.model.db import Session, FileStore
34
35
35 log = logging.getLogger(__name__)
36 log = logging.getLogger(__name__)
36
37
@@ -79,6 +80,22 b' class FileStoreView(BaseAppView):'
79 'access_path': None,
80 'access_path': None,
80 'error': 'File {} is exceeding allowed limit.'.format(filename)}
81 'error': 'File {} is exceeding allowed limit.'.format(filename)}
81
82
83 try:
84 entry = FileStore.create(
85 file_uid=store_fid, filename=metadata["filename"],
86 file_hash=metadata["sha256"], file_size=metadata["size"],
87 file_description='upload attachment',
88 check_acl=False, user_id=self._rhodecode_user.user_id
89 )
90 Session().add(entry)
91 Session().commit()
92 log.debug('Stored upload in DB as %s', entry)
93 except Exception:
94 log.exception('Failed to store file %s', filename)
95 return {'store_fid': None,
96 'access_path': None,
97 'error': 'File {} failed to store in DB.'.format(filename)}
98
82 return {'store_fid': store_fid,
99 return {'store_fid': store_fid,
83 'access_path': h.route_path('download_file', fid=store_fid)}
100 'access_path': h.route_path('download_file', fid=store_fid)}
84
101
@@ -87,9 +104,12 b' class FileStoreView(BaseAppView):'
87 self.load_default_context()
104 self.load_default_context()
88 file_uid = self.request.matchdict['fid']
105 file_uid = self.request.matchdict['fid']
89 log.debug('Requesting FID:%s from store %s', file_uid, self.storage)
106 log.debug('Requesting FID:%s from store %s', file_uid, self.storage)
107
90 if not self.storage.exists(file_uid):
108 if not self.storage.exists(file_uid):
91 log.debug('File with FID:%s not found in the store', file_uid)
109 log.debug('File with FID:%s not found in the store', file_uid)
92 raise HTTPNotFound()
110 raise HTTPNotFound()
93
111
112 FileStore.bump_access_counter(file_uid)
113
94 file_path = self.storage.store_path(file_uid)
114 file_path = self.storage.store_path(file_uid)
95 return FileResponse(file_path)
115 return FileResponse(file_path)
@@ -4893,6 +4893,36 b' class FileStore(Base, BaseModel):'
4893 nullable=True, unique=None, default=None)
4893 nullable=True, unique=None, default=None)
4894 repo_group = relationship('RepoGroup', lazy='joined')
4894 repo_group = relationship('RepoGroup', lazy='joined')
4895
4895
4896 @classmethod
4897 def create(cls, file_uid, filename, file_hash, file_size, file_display_name='',
4898 file_description='', enabled=True, check_acl=True,
4899 user_id=None, scope_repo_id=None, scope_repo_group_id=None):
4900
4901 store_entry = FileStore()
4902 store_entry.file_uid = file_uid
4903 store_entry.file_display_name = file_display_name
4904 store_entry.file_org_name = filename
4905 store_entry.file_size = file_size
4906 store_entry.file_hash = file_hash
4907 store_entry.file_description = file_description
4908
4909 store_entry.check_acl = check_acl
4910 store_entry.enabled = enabled
4911
4912 store_entry.user_id = user_id
4913 store_entry.scope_repo_id = scope_repo_id
4914 store_entry.scope_repo_group_id = scope_repo_group_id
4915 return store_entry
4916
4917 @classmethod
4918 def bump_access_counter(cls, file_uid, commit=True):
4919 FileStore().query()\
4920 .filter(FileStore.file_uid == file_uid)\
4921 .update({FileStore.accessed_count: (FileStore.accessed_count + 1),
4922 FileStore.accessed_on: datetime.datetime.now()})
4923 if commit:
4924 Session().commit()
4925
4896 def __repr__(self):
4926 def __repr__(self):
4897 return '<FileStore({})>'.format(self.file_store_id)
4927 return '<FileStore({})>'.format(self.file_store_id)
4898
4928
General Comments 0
You need to be logged in to leave comments. Login now