From b543121767fc2d1eece383214230ee6cbbf713e9 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Mon, 18 Feb 2008 20:19:36 +0000 Subject: [PATCH] Now Content-Type header is saved upon upload and sent upon download. --- controller/Files.py | 8 +++----- model/File.py | 23 +++++++++++++++-------- model/delta/1.2.0.sql | 3 ++- model/schema.sql | 3 ++- model/test/Test_file.py | 5 ++++- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/controller/Files.py b/controller/Files.py index 2734ee5..a4418bc 100644 --- a/controller/Files.py +++ b/controller/Files.py @@ -234,8 +234,7 @@ class Files( object ): cherrypy.response.headerMap[ u"Content-Disposition" ] = u"attachment; filename=%s" % db_file.filename cherrypy.response.headerMap[ u"Content-Length" ] = db_file.size_bytes -# TODO: send content type -# cherrypy.response.headerMap[ u"Content-Type" ] = u"image/png" + cherrypy.response.headerMap[ u"Content-Type" ] = db_file.content_type def stream(): CHUNK_SIZE = 8192 @@ -323,12 +322,11 @@ class Files( object ): if not uploaded_file: raise Upload_error() -# TODO: grab content type and store it - #print upload.headers.get( "content-type", "MISSING" ) + content_type = upload.headers.get( "content-type" ) # TODO: somehow detect when upload is canceled and abort - db_file = File.create( file_id, notebook_id, note_id, uploaded_file.filename, uploaded_file.file_received_bytes ) + 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 ) uploaded_file.close() diff --git a/model/File.py b/model/File.py index d209897..5b5b961 100644 --- a/model/File.py +++ b/model/File.py @@ -10,7 +10,7 @@ class File( Persistent ): psycopg2 I'm using doesn't have large object support.) """ def __init__( self, object_id, revision = None, notebook_id = None, note_id = None, - filename = None, size_bytes = None ): + filename = None, size_bytes = None, content_type = None ): """ Create a File with the given id. @@ -26,6 +26,8 @@ class File( Persistent ): @param filename: name of the file on the client @type size_bytes: int @param size_bytes: length of the file data in bytes + @type content_type: unicode + @param content_type: value of the Content-Type HTTP header for this file @rtype: File @return: newly constructed File """ @@ -34,9 +36,10 @@ class File( Persistent ): self.__note_id = note_id self.__filename = filename self.__size_bytes = size_bytes + self.__content_type = content_type @staticmethod - def create( object_id, notebook_id = None, note_id = None, filename = None, size_bytes = None ): + def create( object_id, notebook_id = None, note_id = None, filename = None, size_bytes = None, content_type = None ): """ Convenience constructor for creating a new File. @@ -50,11 +53,13 @@ class File( Persistent ): @param filename: name of the file on the client @type size_bytes: int @param size_bytes: length of the file data in bytes + @type content_type: unicode + @param content_type: value of the Content-Type HTTP header for this file @rtype: File @return: newly constructed File """ return File( object_id, notebook_id = notebook_id, note_id = note_id, filename = filename, - size_bytes = size_bytes ) + size_bytes = size_bytes, content_type = content_type ) @staticmethod def sql_load( object_id, revision = None ): @@ -65,7 +70,7 @@ class File( Persistent ): return \ """ select - file.id, file.revision, file.notebook_id, file.note_id, file.filename, size_bytes + file.id, file.revision, file.notebook_id, file.note_id, file.filename, file.size_bytes, file.content_type from file where @@ -83,14 +88,14 @@ class File( Persistent ): return File.sql_id_exists( self.object_id ) def sql_create( self ): - return "insert into file ( id, revision, notebook_id, note_id, filename, size_bytes ) values ( %s, %s, %s, %s, %s, %s );" % \ + return "insert into file ( id, revision, notebook_id, note_id, filename, size_bytes, content_type ) values ( %s, %s, %s, %s, %s, %s, %s );" % \ ( quote( self.object_id ), quote( self.revision ), quote( self.__notebook_id ), quote( self.__note_id ), - quote( self.__filename ), self.__size_bytes or 'null' ) + quote( self.__filename ), self.__size_bytes or 'null', quote( self.__content_type ) ) def sql_update( self ): - return "update file set revision = %s, notebook_id = %s, note_id = %s, filename = %s, size_bytes = %s where id = %s;" % \ + return "update file set revision = %s, notebook_id = %s, note_id = %s, filename = %s, size_bytes = %s, content_type = %s where id = %s;" % \ ( quote( self.revision ), quote( self.__notebook_id ), quote( self.__note_id ), quote( self.__filename ), - self.__size_bytes or 'null', quote( self.object_id ) ) + self.__size_bytes or 'null', quote( self.__content_type ), quote( self.object_id ) ) def to_dict( self ): d = Persistent.to_dict( self ) @@ -99,6 +104,7 @@ class File( Persistent ): note_id = self.__note_id, filename = self.__filename, size_bytes = self.__size_bytes, + content_type = self.__content_type, ) ) return d @@ -107,3 +113,4 @@ class File( Persistent ): note_id = property( lambda self: self.__note_id ) filename = property( lambda self: self.__filename ) size_bytes = property( lambda self: self.__size_bytes ) + content_type = property( lambda self: self.__content_type ) diff --git a/model/delta/1.2.0.sql b/model/delta/1.2.0.sql index 25f72d2..c2b46c9 100644 --- a/model/delta/1.2.0.sql +++ b/model/delta/1.2.0.sql @@ -4,6 +4,7 @@ create table file ( notebook_id text, note_id text, filename text, - size_bytes integer + size_bytes integer, + content_type text ); alter table file add primary key ( id ); diff --git a/model/schema.sql b/model/schema.sql index 77d4b38..a72c9e2 100644 --- a/model/schema.sql +++ b/model/schema.sql @@ -41,7 +41,8 @@ CREATE TABLE file ( notebook_id text, note_id text, filename text, - size_bytes integer + size_bytes integer, + content_type text ); diff --git a/model/test/Test_file.py b/model/test/Test_file.py index c091bef..72aef4e 100644 --- a/model/test/Test_file.py +++ b/model/test/Test_file.py @@ -10,10 +10,11 @@ class Test_file( object ): self.note_id = u"19" self.filename = u"foo.png" self.size_bytes = 2888 + self.content_type = "image/png" self.delta = timedelta( seconds = 1 ) self.file = File.create( self.object_id, self.notebook_id, self.note_id, self.filename, - self.size_bytes ) + self.size_bytes, self.content_type ) def test_create( self ): assert self.file.object_id == self.object_id @@ -21,6 +22,7 @@ class Test_file( object ): assert self.file.note_id == self.note_id assert self.file.filename == self.filename assert self.file.size_bytes == self.size_bytes + assert self.file.content_type == self.content_type def test_to_dict( self ): d = self.file.to_dict() @@ -31,3 +33,4 @@ class Test_file( object ): assert d.get( "note_id" ) == self.note_id assert d.get( "filename" ) == self.filename assert d.get( "size_bytes" ) == self.size_bytes + assert d.get( "content_type" ) == self.content_type