diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index c35d7644d..a077f4b20 100644 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -2559,30 +2559,12 @@ def _upload_to_sg(self, entity_type, entity_id, path, field_name, display_name, params.update(self._auth_params()) - # If we ended up with a unicode string path, we need to encode it - # as a utf-8 string. If we don't, there's a chance that there will - # will be an attempt later on to encode it as an ascii string, and - # that will fail ungracefully if the path contains any non-ascii - # characters. - # - # On Windows, if the path contains non-ascii characters, the calls - # to open later in this method will fail to find the file if given - # a non-ascii-encoded string path. In that case, we're going to have - # to call open on the unicode path, but we'll use the encoded string - # for everything else. - path_to_open = path - if isinstance(path, six.text_type): - path = path.encode("utf-8") - if sys.platform != "win32": - path_to_open = path - if is_thumbnail: url = urllib.parse.urlunparse((self.config.scheme, self.config.server, "/upload/publish_thumbnail", None, None, None)) - params["thumb_image"] = open(path_to_open, "rb") + params["thumb_image"] = open(path, "rb") if field_name == "filmstrip_thumb_image" or field_name == "filmstrip_image": params["filmstrip"] = True - else: url = urllib.parse.urlunparse((self.config.scheme, self.config.server, "/upload/upload_file", None, None, None)) @@ -2596,7 +2578,7 @@ def _upload_to_sg(self, entity_type, entity_id, path, field_name, display_name, if tag_list: params["tag_list"] = tag_list - params["file"] = open(path_to_open, "rb") + params["file"] = open(path, "rb") result = self._send_form(url, params) diff --git "a/tests/No\303\253l.jpg" "b/tests/No\303\253l\343\201\224.jpg" similarity index 100% rename from "tests/No\303\253l.jpg" rename to "tests/No\303\253l\343\201\224.jpg" diff --git a/tests/test_api.py b/tests/test_api.py index 4fdaab038..2866628a3 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -243,7 +243,7 @@ def test_upload_download(self): # test upload of non-ascii, unicode path u_path = os.path.abspath( os.path.expanduser( - glob.glob(os.path.join(six.text_type(this_dir), u'No*l.jpg'))[0] + glob.glob(os.path.join(six.text_type(this_dir), "Noëlご.jpg"))[0] ) ) @@ -310,6 +310,67 @@ def test_upload_download(self): # cleanup os.remove(file_path) + @patch('shotgun_api3.Shotgun._send_form') + def test_upload_to_sg(self, mock_send_form): + """ + Upload an attachment tests for _upload_to_sg() + """ + if "localhost" in self.server_url: + self.skipTest("upload / down tests skipped for localhost") + + self.sg.server_info["s3_direct_uploads_enabled"] = False + mock_send_form.method.assert_called_once() + mock_send_form.return_value = "1\n:123\nasd" + this_dir, _ = os.path.split(__file__) + u_path = os.path.abspath( + os.path.expanduser( + glob.glob(os.path.join(six.text_type(this_dir), "Noëlご.jpg"))[0] + ) + ) + upload_id = self.sg.upload( + "Ticket", + self.ticket['id'], + u_path, + 'attachments', + tag_list="monkeys, everywhere, send, help" + ) + mock_send_form_args, _ = mock_send_form.call_args + display_name_to_send = mock_send_form_args[1].get("display_name", "") + self.assertTrue(isinstance(upload_id, int)) + self.assertFalse( + display_name_to_send.startswith("b'") and + display_name_to_send.endswith("'") + ) + + upload_id = self.sg.upload( + "Ticket", + self.ticket['id'], + u_path, + 'filmstrip_image', + tag_list="monkeys, everywhere, send, help", + ) + self.assertTrue(isinstance(upload_id, int)) + mock_send_form_args, _ = mock_send_form.call_args + display_name_to_send = mock_send_form_args[1].get("display_name", "") + self.assertTrue(isinstance(upload_id, int)) + self.assertFalse( + display_name_to_send.startswith("b'") and + display_name_to_send.endswith("'") + ) + + mock_send_form.method.assert_called_once() + mock_send_form.return_value = "2\nIt can't be upload" + self.assertRaises( + shotgun_api3.ShotgunError, + self.sg.upload, + "Ticket", + self.ticket['id'], + u_path, + 'attachments', + tag_list="monkeys, everywhere, send, help" + ) + self.sg.server_info["s3_direct_uploads_enabled"] = True + def test_upload_thumbnail_in_create(self): """Upload a thumbnail via the create method""" this_dir, _ = os.path.split(__file__)