file-store: save DB entry on upload, and track access times.

This commit is contained in:
Marcin Kuzminski 2019-02-05 12:25:26 +01:00
parent 723799ca1d
commit a1d69a8c7f
2 changed files with 51 additions and 1 deletions

View file

@ -26,11 +26,12 @@ from pyramid.httpexceptions import HTTPFound, HTTPNotFound
from rhodecode.apps._base import BaseAppView
from rhodecode.apps.file_store import utils
from rhodecode.apps.file_store.exceptions import (
FileNotAllowedException,FileOverSizeException)
FileNotAllowedException, FileOverSizeException)
from rhodecode.lib import helpers as h
from rhodecode.lib import audit_logger
from rhodecode.lib.auth import (CSRFRequired, NotAnonymous)
from rhodecode.model.db import Session, FileStore
log = logging.getLogger(__name__)
@ -79,6 +80,22 @@ class FileStoreView(BaseAppView):
'access_path': None,
'error': 'File {} is exceeding allowed limit.'.format(filename)}
try:
entry = FileStore.create(
file_uid=store_fid, filename=metadata["filename"],
file_hash=metadata["sha256"], file_size=metadata["size"],
file_description='upload attachment',
check_acl=False, user_id=self._rhodecode_user.user_id
)
Session().add(entry)
Session().commit()
log.debug('Stored upload in DB as %s', entry)
except Exception:
log.exception('Failed to store file %s', filename)
return {'store_fid': None,
'access_path': None,
'error': 'File {} failed to store in DB.'.format(filename)}
return {'store_fid': store_fid,
'access_path': h.route_path('download_file', fid=store_fid)}
@ -87,9 +104,12 @@ class FileStoreView(BaseAppView):
self.load_default_context()
file_uid = self.request.matchdict['fid']
log.debug('Requesting FID:%s from store %s', file_uid, self.storage)
if not self.storage.exists(file_uid):
log.debug('File with FID:%s not found in the store', file_uid)
raise HTTPNotFound()
FileStore.bump_access_counter(file_uid)
file_path = self.storage.store_path(file_uid)
return FileResponse(file_path)

View file

@ -4893,6 +4893,36 @@ class FileStore(Base, BaseModel):
nullable=True, unique=None, default=None)
repo_group = relationship('RepoGroup', lazy='joined')
@classmethod
def create(cls, file_uid, filename, file_hash, file_size, file_display_name='',
file_description='', enabled=True, check_acl=True,
user_id=None, scope_repo_id=None, scope_repo_group_id=None):
store_entry = FileStore()
store_entry.file_uid = file_uid
store_entry.file_display_name = file_display_name
store_entry.file_org_name = filename
store_entry.file_size = file_size
store_entry.file_hash = file_hash
store_entry.file_description = file_description
store_entry.check_acl = check_acl
store_entry.enabled = enabled
store_entry.user_id = user_id
store_entry.scope_repo_id = scope_repo_id
store_entry.scope_repo_group_id = scope_repo_group_id
return store_entry
@classmethod
def bump_access_counter(cls, file_uid, commit=True):
FileStore().query()\
.filter(FileStore.file_uid == file_uid)\
.update({FileStore.accessed_count: (FileStore.accessed_count + 1),
FileStore.accessed_on: datetime.datetime.now()})
if commit:
Session().commit()
def __repr__(self):
return '<FileStore({})>'.format(self.file_store_id)