Class MediaFile

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<java.io.File>

    public class MediaFile
    extends java.io.File
    Media file with abilities required for syncing with Google Photos.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      MediaFile​(@NotNull java.io.File file)
      Creates a new MediaFile instance from the absolute path of the given File object.
      MediaFile​(@NotNull java.lang.String pathname)
      Creates a new File instance by converting the given pathname string into an abstract pathname.
      MediaFile​(@NotNull java.net.URI uri)
      Creates a new MediaFile instance by converting the given file: URI into an abstract pathname.
      MediaFile​(java.io.File parent, @NotNull java.lang.String child)
      Creates a new MediaFile instance from a parent abstract pathname and a child pathname string.
      MediaFile​(java.lang.String parent, @NotNull java.lang.String child)
      Creates a new MediaFile instance from a parent pathname string and a child pathname string.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static @Nullable java.lang.String extractChecksumStringFromDescription​(@NotNull java.lang.String description)
      Extract a string representation of the checksum from a given media item description string.
      static @Nullable java.time.ZonedDateTime extractLastModifiedDateFromDescription​(@NotNull java.lang.String description)
      Extract a zoned date and time of the last modification of the media file from a given media item description string.
      static java.util.stream.Stream<MediaFile> fileFinder​(java.io.File directory)
      Get a stream all media files in a given directory.
      static java.util.stream.Stream<MediaFile> fileFinder​(java.io.File directory, java.lang.String fileNameRegExp)
      Get a stream of all media files in a given directory.
      java.lang.String generateDescription()
      Generate a media item description string.
      byte[] getContentChecksum()
      Get a checksum of the content of the media file.
      java.lang.String getContentChecksumString()
      Get a checksum of the content of the media file as a string representation of a hexadecimal number.
      java.time.ZonedDateTime getLastModifiedDate()
      Get a zoned date and time of the last modification of the media file.
      boolean isChecksumStringMatching​(java.lang.String checksumString)
      Check whether a given checksum string is matching the content checksum.
      boolean isPhoto()
      Checks if the the media file is a photo.
      boolean isVideo()
      Checks if the the media file is a video.
      void setContentChecksum()
      Compute and set a checksum of the content of the media file.
      • Methods inherited from class java.io.File

        canExecute, canRead, canWrite, compareTo, createNewFile, createTempFile, createTempFile, delete, deleteOnExit, equals, exists, getAbsoluteFile, getAbsolutePath, getCanonicalFile, getCanonicalPath, getFreeSpace, getName, getParent, getParentFile, getPath, getTotalSpace, getUsableSpace, hashCode, isAbsolute, isDirectory, isFile, isHidden, lastModified, length, list, list, listFiles, listFiles, listFiles, listRoots, mkdir, mkdirs, renameTo, setExecutable, setExecutable, setLastModified, setReadable, setReadable, setReadOnly, setWritable, setWritable, toPath, toString, toURI, toURL
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • PHOTO_FILENAME_REGEXP_EXTENSIONS

        public static final java.lang.String PHOTO_FILENAME_REGEXP_EXTENSIONS
        Default regular expression matching the photo files. See https://support.google.com/photos/answer/6193313
        See Also:
        Constant Field Values
      • PHOTO_FILENAME_REGEXP

        public static final java.lang.String PHOTO_FILENAME_REGEXP
        See Also:
        Constant Field Values
      • VIDEO_FILENAME_REGEXP_EXTENSIONS

        public static final java.lang.String VIDEO_FILENAME_REGEXP_EXTENSIONS
        Default regular expression matching the video files. See https://support.google.com/photos/answer/6193313
        See Also:
        Constant Field Values
      • VIDEO_FILENAME_REGEXP

        public static final java.lang.String VIDEO_FILENAME_REGEXP
        See Also:
        Constant Field Values
      • MEDIA_FILENAME_REGEXP

        public static final java.lang.String MEDIA_FILENAME_REGEXP
        Default regular expression matching the media files.
        See Also:
        Constant Field Values
      • CHECKSUM_ALGORITHM

        public static final java.lang.String CHECKSUM_ALGORITHM
        Algorithm of the content checksum.
        See Also:
        Constant Field Values
      • DESCRIPTION_ITEMS_SEPARATOR

        public static final java.lang.String DESCRIPTION_ITEMS_SEPARATOR
        Separator of parsed items in the media items descriptions.
        See Also:
        Constant Field Values
    • Constructor Detail

      • MediaFile

        public MediaFile​(@NotNull
                         @NotNull java.lang.String pathname)
        Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.
        Parameters:
        pathname - A pathname string
        Throws:
        java.lang.NullPointerException - If the pathname argument is null
      • MediaFile

        public MediaFile​(java.lang.String parent,
                         @NotNull
                         @NotNull java.lang.String child)
        Creates a new MediaFile instance from a parent pathname string and a child pathname string.
        Parameters:
        parent - The parent pathname string
        child - The child pathname string
        Throws:
        java.lang.NullPointerException - If child is null
      • MediaFile

        public MediaFile​(java.io.File parent,
                         @NotNull
                         @NotNull java.lang.String child)
        Creates a new MediaFile instance from a parent abstract pathname and a child pathname string.
        Parameters:
        parent - The parent abstract pathname
        child - The child pathname string
        Throws:
        java.lang.NullPointerException - If child is null
      • MediaFile

        public MediaFile​(@NotNull
                         @NotNull java.net.URI uri)
        Creates a new MediaFile instance by converting the given file: URI into an abstract pathname.
        Parameters:
        uri - An absolute, hierarchical URI with a scheme equal to "file", a non-empty path component, and undefined authority, query, and fragment components
        Throws:
        java.lang.NullPointerException - If uri is null
        java.lang.IllegalArgumentException - If the preconditions on the parameter do not hold
      • MediaFile

        public MediaFile​(@NotNull
                         @NotNull java.io.File file)
        Creates a new MediaFile instance from the absolute path of the given File object.
        Parameters:
        file - a file to use
    • Method Detail

      • fileFinder

        public static java.util.stream.Stream<MediaFile> fileFinder​(java.io.File directory)
                                                             throws java.io.FileNotFoundException
        Get a stream all media files in a given directory. The media files are those matching the default regular expression for media files.
        Parameters:
        directory - the directory to search for the media files
        Returns:
        the stream of media files
        Throws:
        java.io.FileNotFoundException - cannot find the directory
      • fileFinder

        public static java.util.stream.Stream<MediaFile> fileFinder​(java.io.File directory,
                                                                    java.lang.String fileNameRegExp)
                                                             throws java.io.FileNotFoundException
        Get a stream of all media files in a given directory.
        Parameters:
        directory - the directory to search for the media files
        fileNameRegExp - a regular expression matching the media files
        Returns:
        the stream of media files
        Throws:
        java.io.FileNotFoundException - cannot find the directory
      • extractChecksumStringFromDescription

        @Nullable
        public static @Nullable java.lang.String extractChecksumStringFromDescription​(@NotNull
                                                                                      @NotNull java.lang.String description)
        Extract a string representation of the checksum from a given media item description string.
        Parameters:
        description - the media item description string
        Returns:
        the string representation of the checksum or null if cannot be extracted
      • extractLastModifiedDateFromDescription

        @Nullable
        public static @Nullable java.time.ZonedDateTime extractLastModifiedDateFromDescription​(@NotNull
                                                                                               @NotNull java.lang.String description)
        Extract a zoned date and time of the last modification of the media file from a given media item description string.
        Parameters:
        description - the media item description string
        Returns:
        zoned date and time of the last modification or null if cannot be extracted
      • isPhoto

        public boolean isPhoto()
        Checks if the the media file is a photo.
        Returns:
        true iff the media file is a photo
      • isVideo

        public boolean isVideo()
        Checks if the the media file is a video.
        Returns:
        true iff the media file is a video
      • setContentChecksum

        public void setContentChecksum()
                                throws java.security.NoSuchAlgorithmException,
                                       java.io.IOException
        Compute and set a checksum of the content of the media file.
        Throws:
        java.security.NoSuchAlgorithmException - cannot find the checksum algorithm
        java.io.IOException - cannot access the file
      • getContentChecksum

        public byte[] getContentChecksum()
                                  throws java.security.NoSuchAlgorithmException,
                                         java.io.IOException
        Get a checksum of the content of the media file.
        Returns:
        the checksum in bytes of the content of the media file
        Throws:
        java.security.NoSuchAlgorithmException - cannot find the checksum algorithm
        java.io.IOException - cannot access the file
      • getContentChecksumString

        public java.lang.String getContentChecksumString()
                                                  throws java.security.NoSuchAlgorithmException,
                                                         java.io.IOException
        Get a checksum of the content of the media file as a string representation of a hexadecimal number.
        Returns:
        the checksum hex-string of the content of the media file
        Throws:
        java.security.NoSuchAlgorithmException - cannot find the checksum algorithm
        java.io.IOException - cannot access the file
      • isChecksumStringMatching

        public boolean isChecksumStringMatching​(java.lang.String checksumString)
                                         throws java.io.IOException,
                                                java.security.NoSuchAlgorithmException
        Check whether a given checksum string is matching the content checksum.
        Parameters:
        checksumString - the checksum string to check
        Returns:
        true iff the checksum string is matching
        Throws:
        java.security.NoSuchAlgorithmException - cannot find the checksum algorithm
        java.io.IOException - cannot access the file
      • getLastModifiedDate

        public java.time.ZonedDateTime getLastModifiedDate()
                                                    throws java.io.IOException
        Get a zoned date and time of the last modification of the media file.
        Returns:
        the zoned date and time of the last modification of the media file
        Throws:
        java.io.IOException - cannot access the file
      • generateDescription

        public java.lang.String generateDescription()
                                             throws java.io.IOException,
                                                    java.security.NoSuchAlgorithmException
        Generate a media item description string.
        Returns:
        the media item description string
        Throws:
        java.security.NoSuchAlgorithmException - cannot find the checksum algorithm
        java.io.IOException - cannot access the file