# Path: analyzer/project.py
# class Project(Model):
# 	keyframe_montage_size = None
# 	keyframe_size = None
# 
# 	def __init__(self, title):
# 		self.name = title
# 
# 		identifier = re.sub('[^a-zA-Z0-9-_*.]', ' ', title.lower())
# 		self.identifier = identifier.replace(" ", "_")
# 
# 		local_base_path = StoragePath.local.base_path(self.identifier)
# 		create_directory(local_base_path)
# 
# 	class Folder(Enum):
# 		frames = 1
# 		keyframes = 2
# 		keyframe_thumbnails = 3
# 		spatio = 4
# 		plots = 5
# 
# 		def __str__(self):
# 			return {
# 				Project.Folder.frames: "frames",
# 				Project.Folder.keyframes: "keyframes",
# 				Project.Folder.keyframe_thumbnails: "keyframe_thumbnails",
# 				Project.Folder.spatio: "spatio_temporal_slices",
# 				Project.Folder.plots: "plots",
# 			}[self]
# 
# 	class File(Enum):
# 		shots = 1
# 		chapters = 2
# 		subtitles = 3
# 		merged_subtitles = 4
# 		original_subtitles = 5
# 		script = 6
# 		keyframe_montage = 7
# 		shot_change_ratio = 8
# 
# 		def __str__(self):
# 			return {
# 				Project.File.shots: "shots.json",
# 				Project.File.chapters: "chapters.json",
# 				Project.File.subtitles: "subtitles.json",
# 				Project.File.original_subtitles: "subtitles.srt",
# 				Project.File.merged_subtitles: "merged_subtitles.json",
# 				Project.File.script: "script.json",
# 				Project.File.keyframe_montage: "keyframe_montage.jpg",
# 				Project.File.shot_change_ratio: "shot_change_ratio.json",
# 			}[self]
# 
# 	def setup(self):
# 		for folder_type in Project.Folder:
# 			self.folder_path(folder_type)
# 
# 	@staticmethod
# 	def file_exists(path):
# 		return os.path.exists(path)
# 
# 	def folder_path(self, folder_type, destination=None, storage_env=StoragePath.local):
# 		assert folder_type is not None
# 
# 		folder = str(folder_type) if type(folder_type) == Project.Folder else folder_type
# 
# 		if destination:
# 			return self.__folder_path(destination, storage_env)
# 		else:
# 			base_path = storage_env.base_path(self.identifier)
# 			default_path = os.path.join(base_path, folder)
# 			return self.__folder_path(default_path, storage_env)
# 
# 	def file_path(self, file_type, destination=None, storage_env=StoragePath.local):
# 		return self.__file_path(str(file_type), destination, storage_env)
# 
# 	def write(self, data, file_type, destination=None):
# 		destination = self.__file_path(str(file_type), destination)
# 		write_json(destination, data)
# 
# 	def read(self, file_type):
# 		path = self.__file_path(str(file_type))
# 		with open(path) as data:
# 			return json.load(data)
# 
# 	def __folder_path(self, folder, storage_env=StoragePath.local):
# 		if storage_env == StoragePath.local:
# 			create_directory(folder)
# 
# 		return folder
# 
# 	def __file_path(self, filename, full_path=None, storage_env=StoragePath.local):
# 		if full_path is None:
# 			base_path = storage_env.base_path(self.identifier)
# 			return os.path.join(base_path, filename)
# 
# 		if os.path.isdir(full_path):
# 			full_path = os.path.join(full_path, filename)
# 
# 		dir = os.path.dirname(full_path)
# 		create_directory(dir)
# 		return full_path
# 
# class StoragePath(Enum):
# 	local = 1
# 	remote = 2
# 
# 	def base_path(self, identifier):
# 		from analyzer.utils import env
# 
# 		if self == StoragePath.local:
# 			data_path = env("DATA_DIR", "data")
# 			return os.path.join(data_path, identifier)
# 		elif self == StoragePath.remote:
# 			return identifier
# 
# Path: analyzer/utils.py
# def env(name, default=None):
# 	return getenv(name, default)
# 
# Path: analyzer/path_utils.py
# def filename(path):
# 	return basename(normpath(path))


import glob
from tqdm import tqdm
from analyzer.project import Project, StoragePath
from minio import Minio
from minio.policy import Policy
from minio.error import ResponseError
from analyzer.utils import env
from analyzer.path_utils import filename
from os.path import join

	def upload_frames(self, project):
		source_path = project.folder_path(Project.Folder.frames)
		remote_path = project.folder_path(Project.Folder.frames, storage_env=StoragePath.remote)

		self.upload_images(source_path, remote_path)

	def upload_keyframes(self, project):
		source_path = project.folder_path(Project.Folder.keyframes)
		remote_path = project.folder_path(Project.Folder.keyframes, storage_env=StoragePath.remote)

		self.upload_images(source_path, remote_path)

	def upload_slices(self, project):
		source_path = project.folder_path(Project.Folder.spatio)
		remote_path = project.folder_path(Project.Folder.spatio, storage_env=StoragePath.remote)

		self.upload_images(source_path, remote_path)

	def upload_keyframe_thumbnails(self, project):
		source_path = project.folder_path(Project.Folder.keyframe_thumbnails)
		remote_path = project.folder_path(Project.Folder.keyframe_thumbnails, storage_env=StoragePath.remote)

		self.upload_images(source_path, remote_path)

	def upload_images(self, source_path, destination_path):
		image_paths = sorted(glob.glob(join(source_path, "*.jpg")))

		progress_bar = tqdm(total=len(image_paths), desc="upload")
		for path in image_paths:

# Path: analyzer/project.py
# class Project(Model):
# 	keyframe_montage_size = None
# 	keyframe_size = None
# 
# 	def __init__(self, title):
# 		self.name = title
# 
# 		identifier = re.sub('[^a-zA-Z0-9-_*.]', ' ', title.lower())
# 		self.identifier = identifier.replace(" ", "_")
# 
# 		local_base_path = StoragePath.local.base_path(self.identifier)
# 		create_directory(local_base_path)
# 
# 	class Folder(Enum):
# 		frames = 1
# 		keyframes = 2
# 		keyframe_thumbnails = 3
# 		spatio = 4
# 		plots = 5
# 
# 		def __str__(self):
# 			return {
# 				Project.Folder.frames: "frames",
# 				Project.Folder.keyframes: "keyframes",
# 				Project.Folder.keyframe_thumbnails: "keyframe_thumbnails",
# 				Project.Folder.spatio: "spatio_temporal_slices",
# 				Project.Folder.plots: "plots",
# 			}[self]
# 
# 	class File(Enum):
# 		shots = 1
# 		chapters = 2
# 		subtitles = 3
# 		merged_subtitles = 4
# 		original_subtitles = 5
# 		script = 6
# 		keyframe_montage = 7
# 		shot_change_ratio = 8
# 
# 		def __str__(self):
# 			return {
# 				Project.File.shots: "shots.json",
# 				Project.File.chapters: "chapters.json",
# 				Project.File.subtitles: "subtitles.json",
# 				Project.File.original_subtitles: "subtitles.srt",
# 				Project.File.merged_subtitles: "merged_subtitles.json",
# 				Project.File.script: "script.json",
# 				Project.File.keyframe_montage: "keyframe_montage.jpg",
# 				Project.File.shot_change_ratio: "shot_change_ratio.json",
# 			}[self]
# 
# 	def setup(self):
# 		for folder_type in Project.Folder:
# 			self.folder_path(folder_type)
# 
# 	@staticmethod
# 	def file_exists(path):
# 		return os.path.exists(path)
# 
# 	def folder_path(self, folder_type, destination=None, storage_env=StoragePath.local):
# 		assert folder_type is not None
# 
# 		folder = str(folder_type) if type(folder_type) == Project.Folder else folder_type
# 
# 		if destination:
# 			return self.__folder_path(destination, storage_env)
# 		else:
# 			base_path = storage_env.base_path(self.identifier)
# 			default_path = os.path.join(base_path, folder)
# 			return self.__folder_path(default_path, storage_env)
# 
# 	def file_path(self, file_type, destination=None, storage_env=StoragePath.local):
# 		return self.__file_path(str(file_type), destination, storage_env)
# 
# 	def write(self, data, file_type, destination=None):
# 		destination = self.__file_path(str(file_type), destination)
# 		write_json(destination, data)
# 
# 	def read(self, file_type):
# 		path = self.__file_path(str(file_type))
# 		with open(path) as data:
# 			return json.load(data)
# 
# 	def __folder_path(self, folder, storage_env=StoragePath.local):
# 		if storage_env == StoragePath.local:
# 			create_directory(folder)
# 
# 		return folder
# 
# 	def __file_path(self, filename, full_path=None, storage_env=StoragePath.local):
# 		if full_path is None:
# 			base_path = storage_env.base_path(self.identifier)
# 			return os.path.join(base_path, filename)
# 
# 		if os.path.isdir(full_path):
# 			full_path = os.path.join(full_path, filename)
# 
# 		dir = os.path.dirname(full_path)
# 		create_directory(dir)
# 		return full_path
# 
# class StoragePath(Enum):
# 	local = 1
# 	remote = 2
# 
# 	def base_path(self, identifier):
# 		from analyzer.utils import env
# 
# 		if self == StoragePath.local:
# 			data_path = env("DATA_DIR", "data")
# 			return os.path.join(data_path, identifier)
# 		elif self == StoragePath.remote:
# 			return identifier
# 
# Path: analyzer/utils.py
# def env(name, default=None):
# 	return getenv(name, default)
# 
# Path: analyzer/path_utils.py
# def filename(path):
# 	return basename(normpath(path))


# Path: analyzer/minio_upload.py
import glob
from tqdm import tqdm
from analyzer.project import Project, StoragePath
from minio import Minio
from minio.policy import Policy
from minio.error import ResponseError
from analyzer.utils import env
from analyzer.path_utils import filename
from os.path import join


	def upload_frames(self, project):
		source_path = project.folder_path(Project.Folder.frames)
		remote_path = project.folder_path(Project.Folder.frames, storage_env=StoragePath.remote)

		self.upload_images(source_path, remote_path)

	def upload_keyframes(self, project):
		source_path = project.folder_path(Project.Folder.keyframes)
		remote_path = project.folder_path(Project.Folder.keyframes, storage_env=StoragePath.remote)

		self.upload_images(source_path, remote_path)

	def upload_slices(self, project):
		source_path = project.folder_path(Project.Folder.spatio)
		remote_path = project.folder_path(Project.Folder.spatio, storage_env=StoragePath.remote)

		self.upload_images(source_path, remote_path)

	def upload_keyframe_thumbnails(self, project):
		source_path = project.folder_path(Project.Folder.keyframe_thumbnails)
		remote_path = project.folder_path(Project.Folder.keyframe_thumbnails, storage_env=StoragePath.remote)

		self.upload_images(source_path, remote_path)

	def upload_images(self, source_path, destination_path):
		image_paths = sorted(glob.glob(join(source_path, "*.jpg")))

		progress_bar = tqdm(total=len(image_paths), desc="upload")
		for path in image_paths:

			remote_path = join(destination_path, filename(path))