Example #1
0
def process_photo(database, file_path, relative_path, mime_type):
    """
    Process JPEG file

    :param database:
    :type database: sqlite3.Connection
    :param file_path: Path to JPEG file
    :type file_path: str
    :param relative_path:
    :type relative_path: str
    :return:
    :rtype: bool
    """
    global unix_epoch

    with open(file_path, 'rb') as fh:

        statinfo = os.stat(file_path)

        tags = image.get_exif(file_path)

        mtime = image.get_original_timestamp(file_path)

        lat, lon = image.get_exif_location(tags)

        try:
            width = int(str(tags['EXIF ExifImageWidth']))
            height = int(str(tags['EXIF ExifImageLength']))
        except:
            width = 0
            height = 0

        sql = 'INSERT INTO photos' \
              + ' (path, dt, size, lat, lon, width, height, mime)' \
              + ' VALUES ("%s", %d, %d, %s, %s, %d, %d, "%s")' \
                % (relative_path,
                   (mtime - unix_epoch).total_seconds(),
                   statinfo.st_size,
                   str(lat) if lat else 'NULL',
                   str(lon) if lon else 'NULL',
                   width,
                   height,
                   mime_type)

        try:
            database.execute(sql)
            database.commit()
        except sqlite3.IntegrityError as e:
            return False

    return True
Example #2
0
def move_file(source_path, target_root, organize_by_date=False, fix_extension=False, mime_type=None):
    """
    Move file to archive directory

    :param source_path:
    :param target_root:
    :param mime_type:
    :return:
    :rtype: bool
    """

    # form target directory path
    if organize_by_date:
        mtime = image.get_original_timestamp(source_path)
        target_dir = os.path.join(target_root, mtime.strftime('%Y/%m/%d'))
    else:
        target_dir = target_root

    # create target directory if not exist
    if not os.path.exists(target_dir):
        os.makedirs(target_dir)

    # create file name
    source_basename = os.path.basename(source_path)

    if fix_extension and mime_type != None:
        target_basename = os.path.splitext(source_basename)[0]
        target_basename += mimetypes.guess_extension(mime_type, False)
    else:
        target_basename = source_basename

    # create full path to target file
    target_path = os.path.join(target_dir, target_basename)

    # move file
    os.rename(source_path, target_path)

    # log result
    logging.info('Copied %s to %s' % (source_path, target_path))

    return True