Ejemplo n.º 1
0
    def get(self, playlist_id, build_job_id):
        playlist = playlists_service.get_playlist(playlist_id)
        project = projects_service.get_project(playlist["project_id"])
        build_job = playlists_service.get_build_job(build_job_id)
        user_service.check_manager_project_access(playlist["project_id"])

        if build_job["status"] != "succeeded":
            return {"error": True, "message": "Build is not finished"}, 400
        else:
            movie_file_path = fs.get_file_path(
                config,
                file_store.get_local_movie_path,
                file_store.open_movie,
                "playlists",
                build_job_id,
                "mp4",
            )
            context_name = slugify.slugify(project["name"], separator="_")
            if project["production_type"] == "tvshow":
                episode = shots_service.get_episode(playlist["episode_id"])
                context_name += "_%s" % slugify.slugify(episode["name"],
                                                        separator="_")
            attachment_filename = "%s_%s_%s.mp4" % (
                slugify.slugify(build_job["created_at"], separator="").replace(
                    "t", "_"),
                context_name,
                slugify.slugify(playlist["name"], separator="_"),
            )
            return flask_send_file(
                movie_file_path,
                conditional=True,
                mimetype="video/mp4",
                as_attachment=True,
                attachment_filename=attachment_filename,
            )
Ejemplo n.º 2
0
def send_storage_file(
    get_local_path,
    open_file,
    prefix,
    preview_file_id,
    extension,
    mimetype="application/octet-stream",
    as_attachment=False,
):
    """
    Send file from storage. If it's not a local storage, cache the file in
    a temporary folder before sending it. It accepts conditional headers.
    """
    file_path = fs.get_file_path_and_file(
        config, get_local_path, open_file, prefix, preview_file_id, extension
    )

    attachment_filename = ""
    if as_attachment:
        attachment_filename = names_service.get_preview_file_name(
            preview_file_id
        )

    try:
        return flask_send_file(
            file_path,
            conditional=True,
            mimetype=mimetype,
            as_attachment=as_attachment,
            attachment_filename=attachment_filename,
        )
    except IOError as e:
        current_app.logger.error(e)
        raise FileNotFound
Ejemplo n.º 3
0
    def get(self, playlist_id):
        playlist = playlists_service.get_playlist(playlist_id)
        project = projects_service.get_project(playlist["project_id"])
        user_service.block_access_to_vendor()
        user_service.check_playlist_access(playlist)
        zip_file_path = playlists_service.build_playlist_zip_file(playlist)

        context_name = slugify.slugify(project["name"], separator="_")
        if project["production_type"] == "tvshow":
            episode_id = playlist["episode_id"]
            if episode_id is not None:
                episode = shots_service.get_episode(playlist["episode_id"])
                episode_name = episode["name"]
            elif playlist["is_for_all"]:
                episode_name = "all assets"
            else:
                episode_name = "main pack"
            context_name += "_%s" % slugify.slugify(
                episode_name, separator="_"
            )
        attachment_filename = "%s_%s.zip" % (
            context_name,
            slugify.slugify(playlist["name"], separator="_"),
        )

        return flask_send_file(
            zip_file_path,
            conditional=True,
            mimetype="application/zip",
            as_attachment=True,
            attachment_filename=attachment_filename,
        )
Ejemplo n.º 4
0
 def get(self, playlist_id):
     playlist = playlists_service.get_playlist(playlist_id)
     user_service.check_project_access(playlist["project_id"])
     zip_file_path = playlists_service.build_playlist_zip_file(playlist)
     attachment_filename = zip_file_path.split(os.sep)[-1]
     return flask_send_file(zip_file_path,
                            conditional=True,
                            mimetype="application/zip",
                            as_attachment=True,
                            attachment_filename=attachment_filename)
Ejemplo n.º 5
0
def send_storage_file(
    get_path,
    open_file,
    prefix,
    preview_file_id,
    extension,
    mimetype="application/octet-stream",
    as_attachment=False
):
    """
    Send file from storage. If it's not a local storage, cache the file in
    a temporary folder before sending it. It accepts conditional headers.
    """
    if config.FS_BACKEND == "local":
        file_path = get_path(prefix, preview_file_id)
    else:
        file_path = os.path.join(
            config.TMP_DIR,
            "cache-%s-%s.%s" % (prefix, preview_file_id, extension)
        )
        if not os.path.exists(file_path):
            with open(file_path, 'wb') as tmp_file:
                for chunk in open_file(prefix, preview_file_id):
                    tmp_file.write(chunk)

    attachment_filename = ""
    if as_attachment:
        attachment_filename = \
            names_service.get_preview_file_name(preview_file_id)

    try:
        return flask_send_file(
            file_path,
            conditional=True,
            mimetype=mimetype,
            as_attachment=as_attachment,
            attachment_filename=attachment_filename
        )
    except IOError as e:
        current_app.logger.error(e)
        return {
            "error": True,
            "message": "File not found for: %s %s" % (
                prefix,
                preview_file_id
            )
        }, 404
    except FileNotFound:
        return {
            "error": True,
            "message": "File not found for: %s %s" % (
                prefix,
                preview_file_id
            )
        }, 404
Ejemplo n.º 6
0
 def get(self, playlist_id):
     playlist = playlists_service.get_playlist(playlist_id)
     user_service.check_project_access(playlist["project_id"])
     movie_file_path = playlists_service.get_playlist_movie_file_path(
         playlist)
     attachment_filename = movie_file_path.split(os.sep)[-1]
     return flask_send_file(movie_file_path,
                            conditional=True,
                            mimetype="video/mp4",
                            as_attachment=True,
                            attachment_filename=attachment_filename)
Ejemplo n.º 7
0
def sendfile(filename):
    if current_app.config["DEBUG"]:
        # Use flask during development
        return flask_send_file(get_archive_path(filename),
                               as_attachment=True)
    else:
        # Use nginx for the heavy lifting on the prod setup
        r = make_response()
        r.headers['Content-Type']=""
        r.headers['Content-Disposition'] = "attachment"
        r.headers['X-Accel-Redirect'] = "/dicom/" + filename
        return r
Ejemplo n.º 8
0
def sendfile(filename):
    if current_app.config["DEBUG"]:
        # Use flask during development
        path = '/'.join([current_app.config['BASE_PATH'], 'dicom', filename])
        return flask_send_file(path,
                               as_attachment=True)
    else:
        # Use nginx for the heavy lifting on the prod setup
        r = make_response()
        r.headers['Content-Type']=""
        r.headers['Content-Disposition'] = "attachment"
        r.headers['X-Accel-Redirect'] = "/dicom/" + filename
        return r
Ejemplo n.º 9
0
    def get(self, playlist_id):
        """
        Download given playlist as zip.
        ---
        tags:
        - Playlists
        parameters:
          - in: path
            name: playlist_id
            required: True
            schema:
                type: UUID
                example: a24a6ea4-ce75-4665-a070-57453082c25
        responses:
            200:
                description: Given playlist downloaded as zip
        """
        playlist = playlists_service.get_playlist(playlist_id)
        project = projects_service.get_project(playlist["project_id"])
        user_service.block_access_to_vendor()
        user_service.check_playlist_access(playlist)
        zip_file_path = playlists_service.build_playlist_zip_file(playlist)

        context_name = slugify.slugify(project["name"], separator="_")
        if project["production_type"] == "tvshow":
            episode_id = playlist["episode_id"]
            if episode_id is not None:
                episode = shots_service.get_episode(playlist["episode_id"])
                episode_name = episode["name"]
            elif playlist["is_for_all"]:
                episode_name = "all assets"
            else:
                episode_name = "main pack"
            context_name += "_%s" % slugify.slugify(
                episode_name, separator="_"
            )
        attachment_filename = "%s_%s.zip" % (
            context_name,
            slugify.slugify(playlist["name"], separator="_"),
        )

        return flask_send_file(
            zip_file_path,
            conditional=True,
            mimetype="application/zip",
            as_attachment=True,
            attachment_filename=attachment_filename,
        )
Ejemplo n.º 10
0
def send_file(file):
    """Create a send file response.

    Arguments:
        file (str): Path of the file.

    Returns:
        (obj): Flask response
    """
    from flask import send_file as flask_send_file
    file_content = open(file, 'rb')
    filename = os.path.basename(file)
    response = flask_send_file(file_content,
                               attachment_filename=filename,
                               as_attachment=True)
    response.headers['Content-Length'] = str(os.path.getsize(file))
    return response
Ejemplo n.º 11
0
 def get(self, attachment_file_id):
     attachment_file = comments_service.get_attachment_file(
         attachment_file_id)
     comment = tasks_service.get_comment(attachment_file["comment_id"])
     task = tasks_service.get_task(comment["object_id"])
     user_service.check_project_access(task["project_id"])
     file_path = comments_service.get_attachment_file_path(attachment_file)
     try:
         return flask_send_file(
             file_path,
             conditional=True,
             mimetype=attachment_file["mimetype"],
             as_attachment=False,
             attachment_filename=attachment_file["name"],
         )
     except:
         abort(404)
Ejemplo n.º 12
0
Archivo: util.py Proyecto: nvlbg/gepify
def send_file(filename, attachment_filename, mimetype, **kwargs):
    response = flask_send_file(filename, mimetype=mimetype)

    try:
        attachment_filename = attachment_filename.encode('ascii')
    except UnicodeEncodeError:
        filenames = {
            'filename':
            unicodedata.normalize('NFKD', attachment_filename).encode(
                'ascii', 'ignore'),
            'filename*':
            "UTF-8''{}".format(url_quote(attachment_filename)),
        }
    else:
        filenames = {'filename': attachment_filename}

    response.headers.set('Content-Disposition', 'attachment', **filenames)
    return response
Ejemplo n.º 13
0
    def get(self, playlist_id):
        playlist = playlists_service.get_playlist(playlist_id)
        user_service.check_project_access(playlist["project_id"])

        if config.ENABLE_JOB_QUEUE:
            current_user = persons_service.get_current_user()
            queue_store.job_queue.enqueue(playlists_service.build_playlist_job,
                                          playlist, current_user["email"])
            return {"job": "running"}
        else:
            movie_file_path = playlists_service.build_playlist_movie_file(
                playlist)
            attachment_filename = movie_file_path.split(os.sep)[-1]
            return flask_send_file(movie_file_path,
                                   conditional=True,
                                   mimetype="video/mp4",
                                   as_attachment=True,
                                   attachment_filename=attachment_filename)
Ejemplo n.º 14
0
def send_storage_file(working_file_id, as_attachment=False):
    """
    Send file from storage. If it's not a local storage, cache the file in
    a temporary folder before sending it. It accepts conditional headers.
    """
    prefix = "working"
    extension = "tmp"
    get_local_path = file_store.get_local_file_path
    open_file = file_store.open_file
    mimetype = "application/octet-stream"

    file_path = fs.get_file_path(
        config, get_local_path, open_file, prefix, working_file_id, extension
    )

    attachment_filename = ""
    if as_attachment:
        attachment_filename = working_file_id

    try:
        return flask_send_file(
            file_path,
            conditional=True,
            mimetype=mimetype,
            as_attachment=as_attachment,
            attachment_filename=attachment_filename,
        )
    except IOError as e:
        current_app.logger.error(e)
        return (
            {
                "error": True,
                "message": "Working file not found for: %s" % working_file_id,
            },
            404,
        )
    except FileNotFound:
        return (
            {
                "error": True,
                "message": "Working file not found for: %s" % working_file_id,
            },
            404,
        )
Ejemplo n.º 15
0
Archivo: util.py Proyecto: nvlbg/gepify
def send_file(filename, attachment_filename, mimetype, **kwargs):
    response = flask_send_file(filename, mimetype=mimetype)

    try:
        attachment_filename = attachment_filename.encode('ascii')
    except UnicodeEncodeError:
        filenames = {
            'filename': unicodedata
                .normalize('NFKD', attachment_filename)
                .encode('ascii', 'ignore'),
            'filename*': "UTF-8''{}".format(
                url_quote(attachment_filename)),
        }
    else:
        filenames = {'filename': attachment_filename}

    response.headers.set(
        'Content-Disposition', 'attachment', **filenames)
    return response
Ejemplo n.º 16
0
    def get(self, playlist_id):
        playlist = playlists_service.get_playlist(playlist_id)
        project = projects_service.get_project(playlist["project_id"])
        user_service.check_playlist_access(playlist)
        zip_file_path = playlists_service.build_playlist_zip_file(playlist)

        context_name = slugify.slugify(project["name"], separator="_")
        if project["production_type"] == "tvshow":
            episode = shots_service.get_episode(playlist["episode_id"])
            context_name += "_%s" % slugify.slugify(episode["name"],
                                                    separator="_")
        attachment_filename = "%s_%s.zip" % (
            context_name,
            slugify.slugify(playlist["name"], separator="_"),
        )

        return flask_send_file(
            zip_file_path,
            conditional=True,
            mimetype="application/zip",
            as_attachment=True,
            attachment_filename=attachment_filename,
        )
Ejemplo n.º 17
0
    def get(self, playlist_id, build_job_id):
        """
        Download given playlist as mp4 after given build job is finished.
        ---
        tags:
        - Playlists
        parameters:
          - in: path
            name: playlist_id
            required: True
            schema:
                type: UUID
                example: a24a6ea4-ce75-4665-a070-57453082c25
          - in: path
            name: build_job_id
            required: True
            schema:
                type: UUID
                example: a24a6ea4-ce75-4665-a070-57453082c25
        responses:
            200:
                description: Given playlist downloaded as mp4
            400: 
                description: Build not finished
        """
        playlist = playlists_service.get_playlist(playlist_id)
        project = projects_service.get_project(playlist["project_id"])
        build_job = playlists_service.get_build_job(build_job_id)
        user_service.check_project_access(playlist["project_id"])

        if build_job["status"] != "succeeded":
            return {"error": True, "message": "Build is not finished"}, 400
        else:
            movie_file_path = fs.get_file_path_and_file(
                config,
                file_store.get_local_movie_path,
                file_store.open_movie,
                "playlists",
                build_job_id,
                "mp4",
            )
            context_name = slugify.slugify(project["name"], separator="_")
            if project["production_type"] == "tvshow":
                episode_id = playlist["episode_id"]
                if episode_id is not None:
                    episode = shots_service.get_episode(playlist["episode_id"])
                    episode_name = episode["name"]
                elif playlist["is_for_all"]:
                    episode_name = "all assets"
                else:
                    episode_name = "main pack"
                context_name += "_%s" % slugify.slugify(
                    episode_name, separator="_"
                )
            attachment_filename = "%s_%s_%s.mp4" % (
                slugify.slugify(build_job["created_at"], separator="").replace(
                    "t", "_"
                ),
                context_name,
                slugify.slugify(playlist["name"], separator="_"),
            )
            return flask_send_file(
                movie_file_path,
                conditional=True,
                mimetype="video/mp4",
                as_attachment=True,
                attachment_filename=attachment_filename,
            )