Beispiel #1
0
    def get(self, instance_id, extension):
        if not self.is_exist(instance_id):
            abort(404)

        if not self.is_allowed(instance_id):
            abort(403)

        folder_path = thumbnail_utils.get_preview_folder_name(
            "originals", instance_id)
        file_name = "%s.%s" % (instance_id, extension)

        return send_from_directory(directory=folder_path, filename=file_name)
Beispiel #2
0
    def post(self, instance_id):
        if not self.is_exist(instance_id):
            abort(404)

        if not self.is_allowed(instance_id):
            abort(403)

        uploaded_file = request.files["file"]

        folder_path = thumbnail_utils.get_preview_folder_name(
            "originals",
            instance_id
        )
        extension = uploaded_file.filename[-4:]
        if extension in [".png", ".jpg"]:
            thumbnail_utils.save_file(
                folder_path,
                instance_id,
                uploaded_file,
                size=None
            )
            if extension == ".jpg":
                thumbnail_utils.convert_jpg_to_png(
                    folder_path,
                    instance_id
                )

            thumbnail_utils.generate_preview_variants(instance_id)
            self.emit_app_preview_event(instance_id)

            return thumbnail_utils.get_preview_url_path(instance_id), 201

        elif extension in [".mp4", ".mov"]:
            from moviepy.editor import VideoFileClip
            file_name = "%s%s" % (instance_id, extension)
            folder = thumbnail_utils.create_folder(folder_path)
            file_path = os.path.join(folder, file_name)
            picture_path = os.path.join(folder, "%s.png" % instance_id)
            uploaded_file.save(file_path + '.tmp')
            clip = VideoFileClip(file_path + '.tmp')

            clip = clip.resize(height=720)
            clip.save_frame(picture_path, round(clip.duration / 2))
            thumbnail_utils.generate_preview_variants(instance_id)

            file_name = "%s%s" % (instance_id, extension)
            clip.write_videofile(os.path.join(folder, instance_id + ".mp4"))
            self.emit_app_preview_event(instance_id)

            return {}, 201

        else:
            abort(400, "Wrong file format")
Beispiel #3
0
    def test_generate_preview_variants(self):
        preview_id = "123413-12312"
        file_path_fixture = self.get_fixture_file_path("thumbnails/th01.png")
        file_name = thumbnail.get_file_name(preview_id)
        folder_path = thumbnail.get_preview_folder_name(
            "originals", preview_id)
        fs.mkdir_p(folder_path)
        fs.copyfile(file_path_fixture, os.path.join(folder_path, file_name))
        thumbnail.generate_preview_variants(preview_id)

        file_path = thumbnail.get_preview_file_path("previews", preview_id)
        self.assertTrue(os.path.exists(file_path))
        self.assertTrue(Image.open(file_path).size, thumbnail.PREVIEW_SIZE)

        folder_path = thumbnail.get_preview_folder_name(
            "thumbnails", preview_id)
        self.assertTrue(os.path.exists(file_path))
        self.assertTrue(Image.open(file_path).size, thumbnail.RECTANGLE_SIZE)

        folder_path = thumbnail.get_preview_folder_name(
            "thumbnails-square", preview_id)
        self.assertTrue(os.path.exists(file_path))
        self.assertTrue(Image.open(file_path).size, thumbnail.SQUARE_SIZE)
Beispiel #4
0
    def get(self, instance_id):
        if not self.is_exist(instance_id):
            abort(404)

        if not self.is_allowed(instance_id):
            abort(403)

        folder_path = thumbnail_utils.get_preview_folder_name(
            self.subfolder, instance_id)
        file_name = thumbnail_utils.get_file_name(instance_id)

        # Use legacy folder name if the file cannot be found.
        if not os.path.exists(os.path.join(folder_path, file_name)):
            folder_path = thumbnail_utils.get_folder_name("preview-files")

        return send_from_directory(directory=folder_path, filename=file_name)
Beispiel #5
0
 def test_get_preview_folder_name(self):
     path = thumbnail.get_preview_folder_name("originals", "123413-12312")
     self.assertEquals(
         path,
         os.path.join(app.config["THUMBNAIL_FOLDER"],
                      "preview-files/originals/123"))