예제 #1
0
    def save_file(self,
                  content=None,
                  decode=False,
                  ignore_existing_file_check=False):
        file_exists = False
        self.content = content

        if decode:
            if isinstance(content, text_type):
                self.content = content.encode("utf-8")

            if b"," in self.content:
                self.content = self.content.split(b",")[1]
            self.content = base64.b64decode(self.content)

        if not self.is_private:
            self.is_private = 0

        self.content_type = mimetypes.guess_type(self.file_name)[0]

        self.file_size = self.check_max_file_size()

        if (self.content_type and "image" in self.content_type
                and frappe.get_system_settings(
                    "strip_exif_metadata_from_uploaded_images")):
            self.content = strip_exif_data(self.content, self.content_type)

        self.content_hash = get_content_hash(self.content)

        duplicate_file = None

        # check if a file exists with the same content hash and is also in the same folder (public or private)
        if not ignore_existing_file_check:
            duplicate_file = frappe.get_value("File", {
                "content_hash": self.content_hash,
                "is_private": self.is_private
            }, ["file_url", "name"],
                                              as_dict=True)

        if duplicate_file:
            file_doc = frappe.get_cached_doc('File', duplicate_file.name)
            if file_doc.exists_on_disk():
                self.file_url = duplicate_file.file_url
                file_exists = True

        if os.path.exists(
                encode(
                    get_files_path(self.file_name,
                                   is_private=self.is_private))):
            self.file_name = get_file_name(self.file_name,
                                           self.content_hash[-6:])

        if not file_exists:
            call_hook_method("before_write_file", file_size=self.file_size)
            write_file_method = get_hook_method('write_file')
            if write_file_method:
                return write_file_method(self)
            return self.save_file_on_filesystem()
예제 #2
0
    def test_strip_exif_data(self):
        original_image = Image.open(
            "../apps/frappe/frappe/tests/data/exif_sample_image.jpg")
        original_image_content = io.open(
            "../apps/frappe/frappe/tests/data/exif_sample_image.jpg",
            mode='rb').read()

        new_image_content = strip_exif_data(original_image_content,
                                            "image/jpeg")
        new_image = Image.open(io.BytesIO(new_image_content))

        self.assertEqual(new_image._getexif(), None)
        self.assertNotEqual(original_image._getexif(), new_image._getexif())