* Quota storage calculation now takes uploaded files into account.
* controller.Files.upload() now updates user quota storage. * When a file is uploaded, the new storage bytes count is returned to the client and displayed.
This commit is contained in:
parent
f6c5071099
commit
3d5baa1e24
|
@ -10,6 +10,7 @@ from Database import Valid_id
|
||||||
from Users import grab_user_id
|
from Users import grab_user_id
|
||||||
from Expire import strongly_expire
|
from Expire import strongly_expire
|
||||||
from model.File import File
|
from model.File import File
|
||||||
|
from model.User import User
|
||||||
from view.Upload_page import Upload_page
|
from view.Upload_page import Upload_page
|
||||||
from view.Blank_page import Blank_page
|
from view.Blank_page import Blank_page
|
||||||
from view.Json import Json
|
from view.Json import Json
|
||||||
|
@ -322,7 +323,6 @@ class Files( object ):
|
||||||
"""
|
"""
|
||||||
global current_uploads, current_uploads_lock
|
global current_uploads, current_uploads_lock
|
||||||
|
|
||||||
# write the file to the database
|
|
||||||
uploaded_file = current_uploads.get( file_id )
|
uploaded_file = current_uploads.get( file_id )
|
||||||
if not uploaded_file:
|
if not uploaded_file:
|
||||||
raise Upload_error()
|
raise Upload_error()
|
||||||
|
@ -340,7 +340,9 @@ class Files( object ):
|
||||||
|
|
||||||
# record metadata on the upload in the database
|
# record metadata on the upload in the database
|
||||||
db_file = File.create( file_id, notebook_id, note_id, uploaded_file.filename, uploaded_file.file_received_bytes, content_type )
|
db_file = File.create( file_id, notebook_id, note_id, uploaded_file.filename, uploaded_file.file_received_bytes, content_type )
|
||||||
self.__database.save( db_file )
|
self.__database.save( db_file, commit = False )
|
||||||
|
self.__users.update_storage( user_id, commit = False )
|
||||||
|
self.__database.commit()
|
||||||
uploaded_file.close()
|
uploaded_file.close()
|
||||||
|
|
||||||
current_uploads_lock.acquire()
|
current_uploads_lock.acquire()
|
||||||
|
@ -405,6 +407,7 @@ class Files( object ):
|
||||||
def stats( self, file_id, user_id = None ):
|
def stats( self, file_id, user_id = None ):
|
||||||
"""
|
"""
|
||||||
Return information on a file that has been completely uploaded and is stored in the database.
|
Return information on a file that has been completely uploaded and is stored in the database.
|
||||||
|
Also return the user's current storage utilization in bytes.
|
||||||
|
|
||||||
@type file_id: unicode
|
@type file_id: unicode
|
||||||
@param file_id: id of the file to report on
|
@param file_id: id of the file to report on
|
||||||
|
@ -414,6 +417,7 @@ class Files( object ):
|
||||||
@return: {
|
@return: {
|
||||||
'filename': filename,
|
'filename': filename,
|
||||||
'size_bytes': filesize,
|
'size_bytes': filesize,
|
||||||
|
'storage_bytes': current sturage usage by user
|
||||||
}
|
}
|
||||||
@raise Access_error: the current user doesn't have access to the notebook that the file is in
|
@raise Access_error: the current user doesn't have access to the notebook that the file is in
|
||||||
"""
|
"""
|
||||||
|
@ -422,9 +426,12 @@ class Files( object ):
|
||||||
if not db_file or not self.__users.check_access( user_id, db_file.notebook_id ):
|
if not db_file or not self.__users.check_access( user_id, db_file.notebook_id ):
|
||||||
raise Access_error()
|
raise Access_error()
|
||||||
|
|
||||||
|
user = self.__database.load( User, user_id )
|
||||||
|
|
||||||
return dict(
|
return dict(
|
||||||
filename = db_file.filename,
|
filename = db_file.filename,
|
||||||
size_bytes = db_file.size_bytes,
|
size_bytes = db_file.size_bytes,
|
||||||
|
storage_bytes = user.storage_bytes,
|
||||||
)
|
)
|
||||||
|
|
||||||
def rename( file_id, filename ):
|
def rename( file_id, filename ):
|
||||||
|
|
|
@ -220,21 +220,33 @@ class User( Persistent ):
|
||||||
|
|
||||||
def sql_calculate_storage( self ):
|
def sql_calculate_storage( self ):
|
||||||
"""
|
"""
|
||||||
Return a SQL string to calculate the total bytes of storage usage by this user. Note that this
|
Return a SQL string to calculate the total bytes of storage usage by this user. This includes
|
||||||
only includes storage for all the user's notes and past revisions in notebooks that they own. It
|
storage for all the user's notes (including past revisions) and their uploaded files. It does
|
||||||
doesn't include storage for the notebooks themselves.
|
not include storage for the notebooks themselves.
|
||||||
"""
|
"""
|
||||||
return \
|
return \
|
||||||
"""
|
"""
|
||||||
select
|
select * from (
|
||||||
coalesce( sum( pg_column_size( note.* ) ), 0 )
|
select
|
||||||
from
|
coalesce( sum( pg_column_size( note.* ) ), 0 )
|
||||||
user_notebook, note
|
from
|
||||||
where
|
user_notebook, note
|
||||||
user_notebook.user_id = %s and
|
where
|
||||||
user_notebook.owner = 't' and
|
user_notebook.user_id = %s and
|
||||||
note.notebook_id = user_notebook.notebook_id;
|
user_notebook.owner = 't' and
|
||||||
""" % quote( self.object_id )
|
note.notebook_id = user_notebook.notebook_id
|
||||||
|
) as note_storage,
|
||||||
|
(
|
||||||
|
select
|
||||||
|
coalesce( sum( file.size_bytes ), 0 )
|
||||||
|
from
|
||||||
|
user_notebook, file
|
||||||
|
where
|
||||||
|
user_notebook.user_id = %s and
|
||||||
|
user_notebook.owner = 't' and
|
||||||
|
file.notebook_id = user_notebook.notebook_id
|
||||||
|
) as file_storage;
|
||||||
|
""" % ( quote( self.object_id ), quote( self.object_id ) )
|
||||||
|
|
||||||
def to_dict( self ):
|
def to_dict( self ):
|
||||||
d = Persistent.to_dict( self )
|
d = Persistent.to_dict( self )
|
||||||
|
|
|
@ -2403,6 +2403,7 @@ function File_link_pulldown( wiki, notebook_id, invoker, editor, link ) {
|
||||||
if ( self.filename_field.value.length == 0 )
|
if ( self.filename_field.value.length == 0 )
|
||||||
self.filename_field.value = result.filename;
|
self.filename_field.value = result.filename;
|
||||||
replaceChildNodes( self.file_size, bytes_to_megabytes( result.size_bytes, true ) );
|
replaceChildNodes( self.file_size, bytes_to_megabytes( result.size_bytes, true ) );
|
||||||
|
self.wiki.display_storage_usage( result.storage_bytes );
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Reference in New Issue