def test_file_versions_download(self):        
        if self.imaging_api.api_client.configuration.on_premise:
            return

        folder = self.temp_folder + '/Storage'
        file1 = 'Storage.txt'
        file2 = 'Folder1/Folder1.txt'

        try:
            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder, self.test_storage)).exists)

            self.imaging_api.copy_file(
                requests.CopyFileRequest(
                    self.original_data_folder + '/' + file1,
                    folder + '/' + file1,
                    self.test_storage,
                    self.test_storage))
            self.imaging_api.copy_file(
                requests.CopyFileRequest(
                    self.original_data_folder + '/' + file2,
                    folder + '/' + file1,
                    self.test_storage,
                    self.test_storage))
            versions = self.imaging_api.get_file_versions(
                requests.GetFileVersionsRequest(
                    folder + '/' + file1, self.test_storage)).value
            recent_version = next(x for x in versions if x.is_latest is True)
            old_version = next(x for x in versions if x.is_latest is not True)

            with open(self.imaging_api.download_file(
                    requests.DownloadFileRequest(folder + '/' + file1, self.test_storage,
                                                 old_version.version_id)), 'r') as old_file:
                self.assertEqual(old_version.size, len(old_file.read()))

            with open(self.imaging_api.download_file(
                    requests.DownloadFileRequest(folder + '/' + file1, self.test_storage,
                                                 recent_version.version_id)), 'r') as old_file:
                self.assertEqual(recent_version.size, len(old_file.read()))

        finally:
            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))
    def append_tiff_from_storage(self):
        """Appends existing TIFF image to another existing TIFF image (i.e. merges TIFF images)"""
        print('Appends existing TIFF image to another existing TIFF image')

        append_file_name = 'Append.tiff'  # Image file name to be appended to original one

        self._upload_sample_image_to_cloud()
        self._upload_image_to_cloud(append_file_name, os.path.join(ImagingBase.EXAMPLE_IMAGES_FOLDER, append_file_name))

        # Update TIFF Image parameters according to fax parameters
        folder = ImagingBase.CLOUD_PATH  # Input file is saved at the Examples folder in the storage
        storage = None  # We are using default Cloud Storage

        append_request = requests.AppendTiffRequest(self._get_sample_image_file_name(), append_file_name, storage,
                                                    folder)

        print('Call AppendTiff')

        self._imaging_api.append_tiff(append_request)

        # Download updated file to local storage
        download_request = requests.DownloadFileRequest(os.path.join(ImagingBase.CLOUD_PATH,
                                                                     self._get_sample_image_file_name()), storage)
        updated_image = self._imaging_api.download_file(download_request)
        self._save_updated_image_to_output('AppendToTiff.tiff', updated_image)
        print()
    def _obtain_object_detection_post_response(self, input_path, out_path,
                                               storage, request_invoker):
        res = self.imaging_api.download_file(
            requests.DownloadFileRequest(input_path, storage))

        response = request_invoker(res, out_path)

        self.assertIsNotNone(response)
        return response
    def test_extract_frame_properties(self):
        """Test extract_image_frame_properties"""

        if not self.EXTENDED_TEST:
            format_extension_test_cases = ['.tiff']
        else:
            format_extension_test_cases = [
                '.cdr',
                '.cmx',
                '.dicom',
                '.djvu',
                '.gif',
                '.odg',
                '.otg',
                '.psd',
                '.webp']

        for format_extension in format_extension_test_cases:
            with self.subTest('format_extension: ' + str(format_extension)):
                frame_id = 1
                folder = self.temp_folder
                storage = self.test_storage

                for input_file in self.multipage_input_test_files:
                    if not str(input_file.name).endswith(format_extension):
                        continue

                    name = input_file.name
                    self._copy_input_file_to_test_folder(name, folder, storage)
                    image_data = self.imaging_api.download_file(requests.DownloadFileRequest(
                        os.path.join(folder, name), storage))

                    print('ExtractImageFrameProperties')
                    print('Input image: {0}; Frame id: {1}'.format(name, frame_id))

                    response_message = self.imaging_api.extract_image_frame_properties(
                        requests.ExtractImageFramePropertiesRequest(image_data, frame_id))

                    self.assertIsNotNone(response_message)
                    if format_extension in self.__formats_without_properties:
                        continue

                    properties_name = TestFramesApi.__get_properties_name(format_extension)
                    result_format_properties = getattr(response_message, properties_name)
                    self.assertIsNotNone(result_format_properties)
예제 #5
0
    def test_convert_from_stream_example(self):
        """ Convert from stream example """

        config = self.imaging_api.api_client.configuration
        imaging_api = ImagingApi(config.api_key['client_secret'],
                                 config.api_key['client_id'], config.host,
                                 config.api_version)
        remote_result_image = 'ExampleFolderPython' + '/' + 'resultImage.jpg'

        try:
            # get local image stream
            local_input_image = os.path.join(self._local_test_folder,
                                             'test.png')
            # convert image from request stream to JPEG and save it to storage
            # please, use outPath parameter for saving the result to storage
            imaging_api.create_converted_image(
                requests.CreateConvertedImageRequest(
                    local_input_image,
                    'jpg',
                    remote_result_image,
                    storage=self.test_storage))

            #  download saved image from storage
            saved_file = imaging_api.download_file(
                requests.DownloadFileRequest(remote_result_image,
                                             storage_name=self.test_storage))
            # TODO: process resulting image from storage

            # convert image from request stream to JPEG and read it from
            # resulting stream
            image_stream = imaging_api.create_converted_image(
                requests.CreateConvertedImageRequest(
                    local_input_image, "jpg", storage=self.test_storage))
            # TODO: process resulting image from response stream

        finally:
            imaging_api.delete_file(
                requests.DeleteFileRequest(remote_result_image,
                                           storage_name=self.test_storage))
    def test_upload_file(self):
        folder = self.temp_folder + '/Storage'
        file = 'Storage.txt'

        try:
            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder, self.test_storage)).exists)

            original_file = self.imaging_api.download_file(
                requests.DownloadFileRequest(
                    self.original_data_folder + '/' + file,
                    self.test_storage))

            result = self.imaging_api.upload_file(
                requests.UploadFileRequest(
                    folder + '/' + file,
                    original_file,
                    self.test_storage))
            self.assertIsNotNone(result)
            self.assertTrue(not result.errors or len(result) == 0)
            self.assertIsNotNone(result.uploaded)
            self.assertEqual(1, len(result.uploaded))
            self.assertEqual(file, result.uploaded[0])

            exist_response = self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder + '/' + file, self.test_storage))
            self.assertIsNotNone(exist_response)
            self.assertFalse(exist_response.is_folder)
            self.assertTrue(exist_response.exists)

            original_file_info = next(
                x for x in self.imaging_api.get_files_list(
                    requests.GetFilesListRequest(
                        self.original_data_folder,
                        self.test_storage)).value if x.name == file)

            uploaded_file_info = next(
                x for x in self.imaging_api.get_files_list(
                    requests.GetFilesListRequest(
                        folder, self.test_storage)).value if x.name == file)
            self.assertEqual(original_file_info.size, uploaded_file_info.size)

            original_file = self.imaging_api.download_file(
                requests.DownloadFileRequest(
                    self.original_data_folder + '/' + file,
                    self.test_storage))

            uploaded_file = self.imaging_api.download_file(
                requests.DownloadFileRequest(
                    folder + '/' + file, self.test_storage))

            with open(original_file, 'r') as original_reader, open(uploaded_file, 'r') as uploaded_reader:
                original_string = original_reader.read()
                copied_string = uploaded_reader.read()
                self.assertEqual(len(original_string), len(copied_string))
                self.assertEqual(len(original_string), original_file_info.size)
                self.assertEqual(original_string, copied_string)
                self.assertEqual(
                    original_string,
                    original_file_info.path.strip('/'))
                self.assertEqual(
                    original_string.replace(
                        self.original_data_folder,
                        folder),
                    uploaded_file_info.path.strip('/'))

        finally:
            self.imaging_api.delete_file(
                requests.DeleteFileRequest(
                    folder + '/' + file,
                    self.test_storage))
            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder + '/' + file,
                        self.test_storage)).exists)

            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder, self.test_storage)).exists)
    def test_move_file(self):
        folder = self.temp_folder + '/Storage'
        file = 'Storage.txt'

        try:
            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder, self.test_storage)).exists)

            self.imaging_api.copy_file(
                requests.CopyFileRequest(
                    self.original_data_folder + '/' + file,
                    folder + '/' + file + '.copied',
                    self.test_storage,
                    self.test_storage))
            exist_response = self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder + '/' + file + '.copied',
                    self.test_storage))
            self.assertIsNotNone(exist_response)
            self.assertFalse(exist_response.is_folder)
            self.assertTrue(exist_response.exists)

            self.imaging_api.move_file(
                requests.MoveFileRequest(
                    folder + '/' + file + '.copied',
                    folder + '/' + file,
                    self.test_storage,
                    self.test_storage))
            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder + '/' + file + '.copied',
                        self.test_storage)).exists)
            exist_response = self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder + '/' + file, self.test_storage))
            self.assertIsNotNone(exist_response)
            self.assertFalse(exist_response.is_folder)
            self.assertTrue(exist_response.exists)

            original_file_info = next(
                x for x in self.imaging_api.get_files_list(
                    requests.GetFilesListRequest(
                        self.original_data_folder,
                        self.test_storage)).value if x.name == file)
            moved_file_info = next(
                x for x in self.imaging_api.get_files_list(
                    requests.GetFilesListRequest(
                        folder, self.test_storage)).value if x.name == file)
            self.assertEqual(original_file_info.size, moved_file_info.size)

            with open(self.imaging_api.download_file(
                    requests.DownloadFileRequest(self.original_data_folder + '/' + file,
                                                 self.test_storage)), 'r') as original_file, \
                    open(self.imaging_api.download_file(requests.DownloadFileRequest(folder + '/' + file,
                                                                                     self.test_storage)),
                         'r') as moved_file:
                original_string = original_file.read()
                moved_string = moved_file.read()
                self.assertEqual(len(original_string), len(moved_string))
                self.assertEqual(len(original_string), original_file_info.size)
                self.assertEqual(original_string, moved_string)
                self.assertEqual(
                    original_string,
                    original_file_info.path.strip('/'))
                self.assertEqual(
                    original_string.replace(
                        self.original_data_folder,
                        folder),
                    moved_file_info.path.strip('/'))

        finally:
            self.imaging_api.delete_file(
                requests.DeleteFileRequest(
                    folder + '/' + file,
                    self.test_storage))
            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder + '/' + file,
                        self.test_storage)).exists)

            if self.imaging_api.object_exists(
                requests.ObjectExistsRequest(
                    folder, self.test_storage)).exists:
                self.imaging_api.delete_folder(
                    requests.DeleteFolderRequest(
                        folder, self.test_storage, True))

            self.assertFalse(
                self.imaging_api.object_exists(
                    requests.ObjectExistsRequest(
                        folder, self.test_storage)).exists)