Ejemplo n.º 1
0
    def add_file_to_project(self, file, projectID):
        if isinstance(file, str):
            fileName = file
        else:
            fileName = file.filename
        ext = fileName[fileName.find("."):]
        print(fileName, "with ext", ext)

        if ext.lower() in (".jpg", ".png"):
            file_type = "image"
        elif ext.lower() in (".mp4", ".webm", ".avi", ".wmv", ".mov"):
            file_type = "video"
        else:
            file_type = "unknown"
            raise InvalidUsage("File specified is not a video/image")

        fileID = str(uuid.uuid4())

        new_file_name = fileID + ext
        new_file_path = os.path.join(DATA_FOLDER, new_file_name)
        print("saving", fileName, "to", new_file_path)

        if isinstance(file, str):
            os.rename(os.path.join(DATA_FOLDER, file), new_file_path)
        else:
            file.save(new_file_path)

        self.dbExec("INSERT INTO FILES VALUES (?,?,?,?,?)",
                    (new_file_name, projectID, file_type, fileName, "{}"))
        return {"original": fileName, "id": new_file_name, "type": file_type}