An object representing a media file.
Parameters: |
|
---|
For example:
from filebrowser.sites import site
from filebrowser.base import FileObject
fileobject = FileObject(os.path.join(site.directory,"testfolder","testimage.jpg"))
version = FileObject(os.path.join(fileobject.versions_basedir, "testfolder", "testimage_medium.jpg"))
Path relative to a storage location (including site.directory):
>>> fileobject.path
'uploads/testfolder/testimage.jpg'
The directory name of pathname path:
>>> fileobject.head
'uploads/testfolder'
Name of the file (including the extension) or name of the folder:
>>> fileobject.filename
'testimage.jpg'
Lower type of filename.
Filename without extension:
>>> fileobject.filename_root
'testimage'
File extension, including the dot. With a folder, the extensions is None:
>>> fileobject.extension
'.jpg'
Mimetype, based on http://docs.python.org/library/mimetypes.html:
>>> fileobject.mimetype
('image/jpeg', None)
Type of the file, as defined with EXTENSIONS:
>>> fileobject.filetype
'Image'
Filesize in Bytes:
>>> fileobject.filesize
870037L
Date, based on time.mktime:
>>> fileobject.date
1299760347.0
Datetime object:
>>> fileobject.datetime
datetime.datetime(2011, 3, 10, 13, 32, 27)
True, if the path exists, False otherwise:
>>> fileobject.exists
True
Path relative to a storage location (including site.directory):
>>> fileobject.path
'uploads/testfolder/testimage.jpg'
Path relative to site.directory:
>>> fileobject.path_relative_directory
'testfolder/testimage.jpg'
Absolute server path (based on storage.path):
>>> fileobject.path_full
'/absolute/path/to/server/location/testfolder/testimage.jpg'
New in version 3.4.
The directory (not including site.directory):
>>> fileobject.dirname
'testfolder'
URL for the file/folder (based on storage.url):
>>> fileobject.url
'/media/uploads/testfolder/testimage.jpg'
The image attributes are only useful if the FileObject represents an image.
Image dimensions as a tuple:
>>> fileobject.dimensions
(1000, 750)
Image width in px:
>>> fileobject.width
1000
Image height in px:
>>> fileobject.height
750
Aspect ratio (float format):
>>> fileobject.aspectratio
1.33534908
Image orientation, either Landscape or Portrait:
>>> fileobject.orientation
'Landscape'
The folder attributes make sense when the FileObject represents a directory (not a file).
Deprecated since version 3.5.3: Use path_relative_directory instead.
Deprecated since version 3.5.3: Use dirname instead.
True, if path is a folder:
>>> fileobject.is_folder
False
True, if the folder is empty. False if the folder is not empty or the FileObject is not a folder:
>>> fileobject.is_empty
False
true if the File is a version of another File:
>>> fileobject.is_version
False
>>> version.is_version
True
The relative path (from storage location) to the main versions folder. Either VERSIONS_BASEDIR or site.directory:
>>> fileobject.versions_basedir
'_versions'
>>> version.versions_basedir
'_versions'
Returns the original FileObject:
>>> fileobject.original
<FileObject: uploads/testfolder/testimage.jpg>
>>> version.original
<FileObject: uploads/testfolder/testimage.jpg>
Get the filename of an original image from a version:
>>> fileobject.original_filename
'testimage.jpg'
>>> version.original_filename
'testimage.jpg'
List all filenames based on VERSIONS:
>>> fileobject.versions()
['_versions/testfolder/testimage_admin_thumbnail.jpg',
'_versions/testfolder/testimage_thumbnail.jpg',
'_versions/testfolder/testimage_small.jpg',
'_versions/testfolder/testimage_medium.jpg',
'_versions/testfolder/testimage_big.jpg',
'_versions/testfolder/testimage_large.jpg']
>>> version.versions()
[]
Note
The versions are not being generated.
List all filenames based on ADMIN_VERSIONS:
>>> fileobject.admin_versions()
['_versions/testfolder/testimage_thumbnail.jpg',
'_versions/testfolder/testimage_small.jpg',
'_versions/testfolder/testimage_medium.jpg',
'_versions/testfolder/testimage_big.jpg',
'_versions/testfolder/testimage_large.jpg']
>>> version.admin_versions()
[]
Note
The versions are not being generated.
Get the filename for a version:
>>> fileobject.version_name("medium")
'testimage_medium.jpg'
Note
The version is not being generated.
Get the path for a version:
>>> fileobject.version_path("medium")
'_versions/testfolder/testimage_medium.jpg'
Note
The version is not being generated.
Generate a version:
>>> fileobject.version_generate("medium")
<FileObject: uploads/testfolder/testimage_medium.jpg>
Please note that a version is only generated, if it does not already exist or if the original image is newer than the existing version.