MediaManager Reference Guide

This guide is for versions Beta 34+


Source code

Background Information: A Technology Stack on Top of a (Neo4j) Graph Database

Class MediaManager

    Helper library for the management of media files (documents and images)
    
nameargumentsreturns
set_media_foldercls, path_name: strNone

        :param path_name:   Location where the media for Content Items is stored, including the final "/"
                                EXAMPLE on Windows: "D:/media/"
                                (notice the forward slashes, even on Windows)
        :return:            None
        
nameargumentsreturns
lookup_file_pathcls, schema_code=None, class_name=None, thumb=Falsestr
        Return the full file path, including the final "/" of media of
        a particular type, identified by the schema_code argument
        TODO: allow media files to have a variety of locations - not just a standard
              one based on their type

        :param schema_code: String identifier used by the various plugins
        :param class_name:  An alternate way to identify the type of the media file.
                                If both schema_code and class_name are specified,
                                class_name prevails
        :param thumb:       If True, then the "thumbnail" version is returned
                                (only applicable to some media types, such as images)
        :return:            EXAMPLES on Windows:
                                "D:/media/documents/"
                                "D:/media/images/resized/"
        
nameargumentsreturns
get_from_text_filecls, path: str, filename: str, encoding="latin-1"str
        Read in and return the contents of the specified TEXT file.

        Note: 'utf8' encoding at times led to problems.
               More info: https://stackoverflow.com/questions/5552555/unicodedecodeerror-invalid-continuation-byte

        :param path:        String that must include a final "/", containing the full path of the file
                                EXAMPLE on Windows:
                                "D:/media/" (notice the forward slashes, even on Windows)
        :param filename:    EXCLUSIVE of path
        :param encoding:    A string such as "latin-1" (aka "iso-8859-1") or "utf8"
        :return:            The contents of the text file, using 'latin-1' encoding
        
nameargumentsreturns
get_from_binary_filecls, path: str, filename: strbytes
        Read in and return the contents of the specified BINARY file

        :param path:        String that must include a final "/", containing the full path of the file
                                EXAMPLE on Windows: "D:/media/" (notice the forward slashes, even on Windows)
        :param filename:    EXCLUSIVE of path.  EXAMPLE: "my pic.jpg"
        :return:            The contents of the binary file
        
nameargumentsreturns
get_binary_contentcls, uri :str, th(str, bytes)
        Fetch and return the contents of a media item stored on a local file.
        In case of error, raise an Exception

        :param uri: String identifier for a media item
        :param th:  If not None, then the thumbnail version is returned (only
                        applicable to images).
                        If the thumbnail version is not found, but the full-size image
                        is present, create and save a thumbnail file, prior to
                        returning the contents of the newly-created file

        :return:    The pair (filename suffix, binary data in the file)
        
nameargumentsreturns
save_into_filecls, contents: str, filename: strNone
        Save the given data into the specified file in the class-wide media folder.  UTF8 encoding is used.
        In case of error, detailed Exceptions are raised

        :param contents:    String to store into the file
        :param filename:    EXCLUSIVE of file path
        :return:            None.  In case of errors, detailed Exceptions are raised
        
nameargumentsreturns
delete_media_filecls, basename: str, suffix: str, schema_code, thumbs=Falsebool
        Delete the specified media file, assumed in a standard location

        :param basename:    File name, exclusive of path and of suffix
        :param suffix:      String such as "htm" (don't include the dot!)
        :param schema_code:
        :param thumbs:
        :return:            True if successful, or False otherwise
        
nameargumentsreturns
delete_filecls, fullname: strbool
        Delete the specified file

        :param fullname:    Full name of the file to delete, including its path
        :return:            True if successful, or False if not found
        
nameargumentsreturns
get_mime_typecls, suffix :strstr
        Return the appropriate MIME type for file content type assumed from the
        filename extension, assuming normal conventions are being followed

        :param suffix:  A string with a filename's suffix (i.e. the "file extension type".)
                            EXAMPLES: "jpg" or "PDF"
        :return:        A string describing the MIME type for files of that particular type
        



Class ImageProcessing

    Utility class for managing images, especially in the context of uploads.

    SIDE NOTE: The "th" format from the Classic BrainAnnex, is:
    "default (largish) thumbs - 3 fit in a row" : width sized to 300

    formats =
    {
        "th": { "description": "default (largish) thumbs - 3 fit in a row",
                "size": 300,
                "affected": "w"
        }
    }
    
nameargumentsreturns
save_thumbnailcls, src_folder: str, filename: str, save_to_folder: str, src_width: int, src_height: intNone
        Make a thumbnail of the specified image, and save it in a file.
        The "th" thumbnail format is being followed.

        :param src_folder:      Full path of folder with the file to resize.  It MUST end with "/"
                                    EXAMPLE (on Windows): "D:/Docs/Brain Annex/media/"
        :param filename:        Name of file to resize.  EXAMPLE: "my image.jpg"
        :param save_to_folder:  Full path of folder where to save the resized file.  It MUST end with "/"
                                    EXAMPLE (on Windows): "D:/Docs/Brain Annex/media/resized/"
        :param src_width:       Pixel width of the original image
        :param src_height:      Pixel height of the original image
        :return:                None.  In case of error, an Exception is raised
        
nameargumentsreturns
scale_down_horizcls, src_folder: str, filename: str, save_to_folder: str, src_width: int, src_height: int, target_width: intNone
        Resize an image to the target WIDTH, and save it in a file.

        :param src_folder:      Full path of folder with the file to resize.  It MUST end with "/"
                                    EXAMPLE (on Windows): "D:/Docs/Brain Annex/media/"
        :param filename:        Name of file to resize.  EXAMPLE: "my image.jpg"
        :param save_to_folder:  Full path of folder where to save the resized file.  It MUST end with "/"
                                    EXAMPLE (on Windows): "D:/Docs/Brain Annex/media/resized/"
        :param src_width:       Pixel width of the original image
        :param src_height:      Pixel height of the original image
        :param target_width:    Desired pixel width of the resized image
        :return:                None.  In case of error, an Exception is raised
        
nameargumentsreturns
get_image_sizecls, source_full_name(int, int)
        Return the size of the given image.

        :param source_full_name:    EXAMPLE (on Windows): "D:/Docs/Brain Annex/media/resized/my image.jpg"
        :return:                    The pair (width, height) with the image dimensions in pixels.  In case of error, an Exception is raised
        
nameargumentsreturns
process_uploaded_imagecls, media_folder :str, basename :str, suffix :strdict
        If possible, obtain the size of the image, resize it to a thumbnail,
        save the thumbnail in the "resized/" subfolder of the specified media folder;
        not all images (such as SVG's) can be resized.

        Return a dictionary of additional image-specific properties that will go in the database.

        :param media_folder:Name of the folder (including the final "/") where the media files are located.
                                The resized version will go in a "resized" subfolder of it.
                                EXAMPLE (on Windows):  "D:/Docs/media/
        :param basename:    EXAMPLE: "my image"
        :param suffix:      EXAMPLE: "jpg"  .  It's ok to be an empty string

        :return:            A dictionary of extra properties to store in database, containing some or all of
                                the following keys: "caption", "width", "height"
        
nameargumentsreturns
describe_imagecls, source_full_nameNone
        Print out some info about the given image:
        the file format, the pixel format, the image size and (if any) the color palette

        :param source_full_name:    EXAMPLE (on Windows): "D:/Docs/media/resized/my image.jpg"
        :return:                    None.  In case of error, an Exception is raised