MediaManager Reference Guide

This guide is for versions Beta 47+


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)
    
    Static class that doesn't get instantiated;
    however, it must be initialized with calls to set_media_folder() and set_default_folders()
    
nameargumentsreturns
set_media_folderpath_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
set_default_foldersfolder_dict :dictNone

        :param folder_dict:
        :return:
        
nameargumentsreturns
default_file_pathclass_name :str, thumb=Falsestr
        Return the default file path, including the final "/", of the media files associated to the given schema Class

        :param class_name:  The name of the Class of the media item of interest
        :param thumb:       If True, then the "thumbnail" version is returned
                                (only applicable to some media types, such as images)
        :return:            The full file path, including the final "/"
                            EXAMPLES on Windows:
                                "D:/media/documents/"
                                "D:/media/images/resized/"
        
nameargumentsreturns
retrieve_full_pathuri :str, thumb=Falsestr
        Return the full path for the specified media file or, if requested, for its thumbnail image.
        Includes the final "/"
        EXAMPLE (on Windows):  "D:/BACKUP_media/images/resized/"

        :param uri:         Unique identifier string for the Media Item of Interest
        :param thumb:       If True, return the folder for the thumbnail image instead
        :return:            EXAMPLES on Windows:
                                "D:/media/documents/"
                                "D:/media/images/resized/"
        
nameargumentsreturns
lookup_media_fileuri :str, thumb=False(str, str, str)

        :param uri:     Unique identifier for the Media Item of Interest
        :param thumb:   If True, return the folder for the thumbnail image instead;
                                ignored if the file suffix is "svg" (regardless of case),
                                because SVG files cannot be resized
        :return:        The triplet (filepath, basename, suffix)
                                Notes:  filepath ends with a "/"
                                        the basename is exclusive of path and of suffix
                                        the suffix does NOT include the dot
                                EXAMPLE:
                                    ("D:/media/my_media_folder/images/", "snap1", "jpg")
        
nameargumentsreturns
get_full_filenameuri : str, thumb=Falsestr

        :param uri:     Unique identifier for the Media Item of Interest
        :param thumb:   If True, return the folder for the thumbnail image instead;
                                ignored if the file suffix is "svg" (regardless of case),
                                because SVG files cannot be resized
        :return:        EXAMPLE: "D:/media/my_media_folder/images/Tahiti vacation/"
        
nameargumentsreturns
get_from_text_filefilename: str, path="", 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 filename:    FULL filename, INCLUDING path - unless path is passed in the following argument
                                EXAMPLE on Windows:
                                "D:/media/my_file.txt"   (notice the forward slashes, even on Windows)
        :param path:        [OPTIONAL] String to prefix to the filename argument, above
        :param encoding:    A string such as "latin-1" (aka "iso-8859-1") or "utf8"
        :return:            The contents of the text file, using the requested encoding
        
nameargumentsreturns
get_from_binary_filepath: 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_contenturi :str, th(str, bytes)
        Fetch and return the contents of a media item stored in 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
before_update_contenturi :str, set_dict :dict, class_name :strNone
        Invoked before a Media Item of this type gets updated in the database

        :param uri:         Unique identifier for the Media Item of Interest
        :param set_dict:    A dict of field values to eventually set into the database
        :param class_name:  (Redundant, since implied by the uri)
        :return:            None
        
nameargumentsreturns
save_into_filecontents: str, filename: str, class_name :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
        :param class_name:  Needed to determine the default folder location (which is based on class_name)
        :return:            None.  In case of errors, detailed Exceptions are raised
        
nameargumentsreturns
delete_media_fileuri: str, thumb=Falsebool
        Delete the specified media file, assumed in a standard location

        :param uri:         Unique identifier for the Media Item of Interest
        :param thumb:       If True, then the "thumbnail" version is deleted
                                (only applicable to some media types, such as images)
        :return:            True if successful, or False otherwise
        
nameargumentsreturns
delete_filefullname: strbool
        Delete the specified file

        :param fullname:    Full name of the file to delete, including its path
        :return:            True if successful, or False if file was not found
        
nameargumentsreturns
move_filesrc: str, dest: strNone
        Rename (move) the specified file.
        An Exception is raised if the file was not found,
        or if another file with new name already exists

        :param src:    Old full name (incl. path) of the file to rename
        :param dest:   New full name (incl. path) of the file to rename
        :return:       None
        
nameargumentsreturns
check_file_namefilename :strstr
        Check the given filename against a list of acceptable filename characters, based on a slightly-expanded
        (but still conservative) version of the POSIX portable file name character set
        https://www.ibm.com/docs/en/zos/3.1.0?topic=locales-posix-portable-file-name-character-set

        :return:    The first non-allowed character, if applicable;
                        if all characters are good, return ""
        
nameargumentsreturns
get_mime_typesuffix :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_thumbnailsrc_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_horizsrc_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_sizesource_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_imagemedia_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_imagesource_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