def copy_to_s3(file_uuid): try: # do additional processing,i.e add certain effects print("Copying to S3 file uuid" + file_uuid) file = File(file_uuid); file.copy(None, 'bucner123', 'true'); except Exception, e: print (e.message);
def __init__(self, cdn_url_or_file_id): matches = cdn_url_or_file_id.startswith('links/') if matches: self.s3_path = cdn_url_or_file_id self.uuid = None else: self.s3_path = None File.__init__(self, cdn_url_or_file_id)
def copy_to_s3(file_uuid): try: # do additional processing,i.e add certain effects print("Copying to S3 file uuid " + file_uuid) logger.info("In the process of copying fileUUID " + str(file_uuid)) file = File(file_uuid) response = file.copy(None, 'bucner123', 'true') except Exception, e: logger.info("An error while copying file to s3 storage: " + str(e))
def test_value_error_when_uuid_is_bad(self): file_serialized_invalid = "blah" self.assertRaises(InvalidParamError, File, file_serialized_invalid) file_serialized_valid = "3addab78-6368-4c55-ac08-22412b6a2a4c" file_ = File(file_serialized_valid) self.assertRaises(InvalidParamError, setattr, file_, "uuid", file_serialized_invalid) self.assertRaises(InvalidParamError, setattr, file_, "uuid", file_serialized_valid + file_serialized_invalid) file_.uuid = file_serialized_valid
def test_value_error_when_uuid_is_bad(self): file_serialized_invalid = 'blah' self.assertRaises(InvalidParamError, File, file_serialized_invalid) file_serialized_valid = '3addab78-6368-4c55-ac08-22412b6a2a4c' file_ = File(file_serialized_valid) self.assertRaises(InvalidParamError, setattr, file_, 'uuid', file_serialized_invalid) self.assertRaises(InvalidParamError, setattr, file_, 'uuid', file_serialized_valid + file_serialized_invalid) file_.uuid = file_serialized_valid
def _wait_if_needed(arg_namespace, check_func, error_msg): if not arg_namespace.wait: return for path in arg_namespace.paths: file_ = File(path) timeout = arg_namespace.timeout time_started = time.time() while not check_func(file_): if time.time() - time_started > timeout: raise TimeoutError(error_msg) file_.update_info() time.sleep(0.1)
def test_successful_upload_from_url_sync_store(self): file = File.upload_from_url_sync( 'https://github.com/images/error/angry_unicorn.png', store=True, interval=1) self.assertIsInstance(file, File) self.assertIsNotNone(file.datetime_stored())
def test_successful_upload_from_url_sync_with_filename(self): file = File.upload_from_url_sync( 'https://github.com/images/error/angry_unicorn.png', filename='meh.png', interval=1) self.assertIsInstance(file, File) self.assertEqual(file.filename(), 'meh.png')
def upload_from_url(arg_namespace): if not _check_upload_args(arg_namespace): return file_from_url = File.upload_from_url(arg_namespace.url) pprint(file_from_url) if arg_namespace.wait or arg_namespace.store: timeout = arg_namespace.timeout time_started = time.time() while time.time() - time_started < timeout: status = file_from_url.update_info()['status'] if status == 'success': break if status in ('failed', 'error'): raise UploadError('could not upload file from url: {0}'.format( file_from_url.info())) time.sleep(1) else: raise TimeoutError('timed out during upload') if arg_namespace.store or arg_namespace.info: file_ = file_from_url.get_file() if file_ is None: pprint('Cannot store or get info.') return _handle_uploaded_file(file_, arg_namespace)
def test_create_local_copy(self): response = self.f.create_local_copy() self.assertEqual('file', response['type']) response = self.f.create_local_copy(effects='resize/50x/') self.assertEqual('file', response['type']) response = self.f.create_local_copy(effects='resize/50x/', store=True) file = File(response['result']['uuid']) time.sleep(2) self.assertEqual(file.is_stored(), True) response = self.f.create_local_copy(store=False) file = File(response['result']['uuid']) time.sleep(2) self.assertEqual(file.is_stored(), False)
def setUpClass(cls): conf.pub_key = 'demopublickey' conf.secret = 'demoprivatekey' # create file to copy from file_from_url = File.upload_from_url( 'https://github.com/images/error/angry_unicorn.png' ) timeout = 30 time_started = time.time() while time.time() - time_started < timeout: status = file_from_url.update_info()['status'] if status in ('success', 'failed', 'error'): break time.sleep(1) cls.f = file_from_url.get_file() time_started = time.time() while time.time() - time_started < timeout: if cls.f.is_ready(): break time.sleep(1) cls.f.update_info()
def upload_from_url(arg_namespace): if not _check_upload_args(arg_namespace): return file_from_url = File.upload_from_url(arg_namespace.url) pprint(file_from_url) if arg_namespace.wait or arg_namespace.store: timeout = arg_namespace.timeout time_started = time.time() while time.time() - time_started < timeout: status = file_from_url.update_info()['status'] if status == 'success': break if status in ('failed', 'error'): raise UploadError( 'could not upload file from url: {0}'.format( file_from_url.info()) ) time.sleep(1) else: raise TimeoutError('timed out during upload') if arg_namespace.store or arg_namespace.info: file_ = file_from_url.get_file() if file_ is None: pprint('Cannot store or get info.') return _handle_uploaded_file(file_, arg_namespace)
def test_successful_upload_from_url_sync_dont_store(self): file = File.upload_from_url_sync(self.image_url, store=False, interval=1) self.assertIsInstance(file, File) self.assertEqual(file.filename(), 'Octocat.png') self.assertIsNone(file.datetime_stored())
def test_successful_upload_from_url_sync_with_filename(self): file = File.upload_from_url_sync( 'https://github.com/images/error/angry_unicorn.png', filename='meh.png', interval=1 ) self.assertIsInstance(file, File) self.assertEqual(file.filename(), 'meh.png')
def test_successful_upload_from_url_sync_store(self): file = File.upload_from_url_sync( 'https://github.com/images/error/angry_unicorn.png', store=True, interval=1 ) self.assertIsInstance(file, File) self.assertIsNotNone(file.datetime_stored())
def upload_tmp_txt_file(content=''): conf.pub_key = 'demopublickey' tmp_txt_file = NamedTemporaryFile(mode='wb', delete=False) tmp_txt_file.write(content.encode('utf-8')) tmp_txt_file.close() with open(tmp_txt_file.name, 'rb') as fh: file_ = File.upload(fh, store=False) return file_
def test_successful_upload_from_url(self): file_from_url = File.upload_from_url(self.image_url) timeout = 30 time_started = time.time() while time.time() - time_started < timeout: status = file_from_url.update_info()['status'] if status in ('success', 'failed', 'error'): break time.sleep(1) self.assertIsInstance(file_from_url.get_file(), File)
def test_successful_upload_from_url(self): file_from_url = File.upload_from_url( 'https://github.com/images/error/angry_unicorn.png') timeout = 30 time_started = time.time() while time.time() - time_started < timeout: status = file_from_url.update_info()['status'] if status in ('success', 'failed', 'error'): break time.sleep(1) self.assertIsInstance(file_from_url.get_file(), File)
def test_successful_upload_from_url(self): file_from_url = File.upload_from_url( 'https://github.com/images/error/angry_unicorn.png' ) timeout = 30 time_started = time.time() while time.time() - time_started < timeout: status = file_from_url.update_info()['status'] if status in ('success', 'failed', 'error'): break time.sleep(1) self.assertIsInstance(file_from_url.get_file(), File)
class FileDateMethodsTest(unittest.TestCase): def setUp(self): self.file = File('a771f854-c2cb-408a-8c36-71af77811f3b') def test_datetime_stored_is_none(self): self.file._info_cache = {'datetime_stored': ''} self.assertIsNone(self.file.datetime_stored()) def test_datetime_stored_is_utc(self): self.file._info_cache = {'datetime_stored': '2013-02-05T12:56:12.006Z'} expected_datetime = datetime.datetime(year=2013, month=2, day=5, hour=12, minute=56, second=12, microsecond=6000, tzinfo=tzutc()) self.assertEqual(self.file.datetime_stored(), expected_datetime) def test_datetime_removed_is_none(self): self.file._info_cache = {} self.assertIsNone(self.file.datetime_removed()) def test_datetime_removed_is_utc(self): self.file._info_cache = { 'datetime_removed': '2013-02-05T12:56:12.006Z' } expected_datetime = datetime.datetime(year=2013, month=2, day=5, hour=12, minute=56, second=12, microsecond=6000, tzinfo=tzutc()) self.assertEqual(self.file.datetime_removed(), expected_datetime) def test_datetime_uploaded_is_none(self): self.file._info_cache = {'datetime_uploaded': None} self.assertIsNone(self.file.datetime_uploaded()) def test_datetime_uploaded_is_utc(self): self.file._info_cache = { 'datetime_uploaded': '2013-02-05T12:56:12.006Z' } expected_datetime = datetime.datetime(year=2013, month=2, day=5, hour=12, minute=56, second=12, microsecond=6000, tzinfo=tzutc()) self.assertEqual(self.file.datetime_uploaded(), expected_datetime)
def test_group_successfully_created(self, request): json_response = """{ "id": "0513dda0-582f-447d-846f-096e5df9e2bb~1", "files_count": 1, "files": [ {"uuid": "0cea5a61-f976-47d9-815a-e787f52aeba1"} ] } """ request.return_value = MockResponse(status=200, data=json_response) files = (File('0cea5a61-f976-47d9-815a-e787f52aeba1'), ) group = FileGroup.create(files) self.assertIsInstance(group, FileGroup) self.assertEqual(len(group), 1) self.assertEqual(group[0].uuid, '0cea5a61-f976-47d9-815a-e787f52aeba1')
def setUpClass(cls): conf.pub_key = 'demopublickey' conf.secret = 'demoprivatekey' # create file to copy from file_from_url = File.upload_from_url( 'https://github.com/images/error/angry_unicorn.png' ) timeout = 30 time_started = time.time() while time.time() - time_started < timeout: status = file_from_url.update_info()['status'] if status in ('success', 'failed', 'error'): break time.sleep(1) cls.f = file_from_url.get_file()
def setUpClass(cls): super(FileCopyTest, cls).setUpClass() # create file to copy from file_from_url = File.upload_from_url(cls.image_url) timeout = 30 time_started = time.time() while time.time() - time_started < timeout: status = file_from_url.update_info()['status'] if status in ('success', 'failed', 'error'): break time.sleep(1) cls.f = file_from_url.get_file() time_started = time.time() while time.time() - time_started < timeout: if cls.f.is_ready(): break time.sleep(1) cls.f.update_info()
def setUpClass(cls): super(FileCopyTest, cls).setUpClass() # create file to copy from file_from_url = File.upload_from_url( 'https://github.com/images/error/angry_unicorn.png' ) timeout = 30 time_started = time.time() while time.time() - time_started < timeout: status = file_from_url.update_info()['status'] if status in ('success', 'failed', 'error'): break time.sleep(1) cls.f = file_from_url.get_file() time_started = time.time() while time.time() - time_started < timeout: if cls.f.is_ready(): break time.sleep(1) cls.f.update_info()
class FileDateMethodsTest(unittest.TestCase): def setUp(self): self.file = File('a771f854-c2cb-408a-8c36-71af77811f3b') def test_datetime_stored_is_none(self): self.file._info_cache = {'datetime_stored': ''} self.assertIsNone(self.file.datetime_stored()) def test_datetime_stored_is_utc(self): self.file._info_cache = {'datetime_stored': '2013-02-05T12:56:12.006Z'} expected_datetime = datetime.datetime( year=2013, month=2, day=5, hour=12, minute=56, second=12, microsecond=6000, tzinfo=tzutc()) self.assertEqual(self.file.datetime_stored(), expected_datetime) def test_datetime_removed_is_none(self): self.file._info_cache = {} self.assertIsNone(self.file.datetime_removed()) def test_datetime_removed_is_utc(self): self.file._info_cache = {'datetime_removed': '2013-02-05T12:56:12.006Z'} expected_datetime = datetime.datetime( year=2013, month=2, day=5, hour=12, minute=56, second=12, microsecond=6000, tzinfo=tzutc()) self.assertEqual(self.file.datetime_removed(), expected_datetime) def test_datetime_uploaded_is_none(self): self.file._info_cache = {'datetime_uploaded': None} self.assertIsNone(self.file.datetime_uploaded()) def test_datetime_uploaded_is_utc(self): self.file._info_cache = {'datetime_uploaded': '2013-02-05T12:56:12.006Z'} expected_datetime = datetime.datetime( year=2013, month=2, day=5, hour=12, minute=56, second=12, microsecond=6000, tzinfo=tzutc()) self.assertEqual(self.file.datetime_uploaded(), expected_datetime)
def setUp(self): self.file = File('a771f854-c2cb-408a-8c36-71af77811f3b')
def test_remote_copy_source(self, request): request.return_value = {} # uuid with no effects f = File('a771f854-c2cb-408a-8c36-71af77811f3b') f.copy(target='tgt') request.assert_called_with('POST', 'files/', data={ "source": "a771f854-c2cb-408a-8c36-71af77811f3b/", "target": "tgt"}) # uuid with effects f = File('a771f854-c2cb-408a-8c36-71af77811f3b') f.copy(target='tgt', effects='resize/1x1/') request.assert_called_with('POST', 'files/', data={ "source": "a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/1x1/", "target": "tgt"}) # cdn url with no effects f = File('a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/2x2/') f.copy(target='tgt') request.assert_called_with('POST', 'files/', data={ "source": "a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/2x2/", "target": "tgt"}) # cdn url with effects f = File('a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/3x3/') f.copy(target='tgt', effects='flip/') request.assert_called_with('POST', 'files/', data={ "source": "a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/3x3/-/flip/", "target": "tgt"})
def get_file(arg_namespace): pprint(File(arg_namespace.path).info())
def upload(arg_namespace): if not _check_upload_args(arg_namespace): return with open(arg_namespace.filename, 'rb') as fh: file_ = File.upload(fh) _handle_uploaded_file(file_, arg_namespace)
def test_uuid_and_arbitrary_domain(self): file_serialized = 'http://example.com/3addab78-6368-4c55-ac08-22412b6a2a4c/' expected_cdn_url = 'http://www.ucarecdn.com/3addab78-6368-4c55-ac08-22412b6a2a4c/' file_ = File(file_serialized) self.assertEqual(file_.cdn_url, expected_cdn_url)
def test_invalid_request_error_if_iterable_is_dict(self): files = { 'file_1': File('6c5e9526-b0fe-4739-8975-72e8d5ee6342'), 'file_2': File('a771f854-c2cb-408a-8c36-71af77811f3b'), } self.assertRaises(InvalidRequestError, FileGroup.create, files)
def test_uuid_and_crop_effect(self): file_serialized = 'cde35b21-c5e1-4ed4-b2fc-d4ef4b0538b0/-/crop/296x445/251,81/' expected_cdn_url = 'https://ucarecdn.com/cde35b21-c5e1-4ed4-b2fc-d4ef4b0538b0/-/crop/296x445/251,81/' file_ = File(file_serialized) self.assertEqual(file_.cdn_url, expected_cdn_url)
def test_successful_upload_from_url_sync(self): file_from_url = File.upload_from_url( 'https://github.com/images/error/angry_unicorn.png' ) file = file_from_url.wait(interval=1, until_ready=False) self.assertIsInstance(file, File)
def test_get_some_token(self): file_from_url = File.upload_from_url( 'https://github.com/images/error/angry_unicorn.png' ) self.assertTrue(file_from_url.token)
def test_successful_upload_when_file_is_opened_in_binary_mode(self): with open(self.tmp_txt_file.name, 'rb') as fh: file_ = File.upload(fh) self.assertIsInstance(file_, File)
def create_group(arg_namespace): files = [File(uuid) for uuid in arg_namespace.paths] group = FileGroup.create(files) pprint(group.info())
def test_only_uuid(self): file_serialized = '3addab78-6368-4c55-ac08-22412b6a2a4c' expected_cdn_url = 'http://www.ucarecdn.com/3addab78-6368-4c55-ac08-22412b6a2a4c/' file_ = File(file_serialized) self.assertEqual(file_.cdn_url, expected_cdn_url)
def test_invalid_request_error_if_non_file_instance(self): files = ( File('6c5e9526-b0fe-4739-8975-72e8d5ee6342'), None, ) self.assertRaises(InvalidRequestError, FileGroup.create, files)
def test_remote_copy_source(self, request): request.return_value = {} # uuid with no effects f = File("a771f854-c2cb-408a-8c36-71af77811f3b") f.copy(target="tgt") request.assert_called_with( "POST", "files/", data={"source": "a771f854-c2cb-408a-8c36-71af77811f3b/", "target": "tgt"} ) # uuid with effects f = File("a771f854-c2cb-408a-8c36-71af77811f3b") f.copy(target="tgt", effects="resize/1x1/") request.assert_called_with( "POST", "files/", data={"source": "a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/1x1/", "target": "tgt"} ) # cdn url with no effects f = File("a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/2x2/") f.copy(target="tgt") request.assert_called_with( "POST", "files/", data={"source": "a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/2x2/", "target": "tgt"} ) # cdn url with effects f = File("a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/3x3/") f.copy(target="tgt", effects="flip/") request.assert_called_with( "POST", "files/", data={"source": "a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/3x3/-/flip/", "target": "tgt"}, )
def test_get_some_token(self): file_from_url = File.upload_from_url( 'https://github.com/images/error/angry_unicorn.png') self.assertTrue(file_from_url.token)
def test_uuid_and_crop_effect_and_arbitrary_domain(self): file_serialized = 'http://www.ucarecdn.com/cde35b21-c5e1-4ed4-b2fc-d4ef4b0538b0/-/crop/296x445/251,81/' expected_cdn_url = file_serialized file_ = File(file_serialized) self.assertEqual(file_.cdn_url, expected_cdn_url)
def test_successful_upload_from_url_sync(self): file_from_url = File.upload_from_url( 'https://github.com/images/error/angry_unicorn.png') file = file_from_url.wait(interval=1, until_ready=False) self.assertIsInstance(file, File)
def test_create_remote_copy(self, request): request.return_value = {} # uuid with no effects f = File('a771f854-c2cb-408a-8c36-71af77811f3b') f.create_remote_copy(target='tgt') request.assert_called_with('POST', 'files/', data={ "source": "a771f854-c2cb-408a-8c36-71af77811f3b/", "target": "tgt"}) # uuid with effects f = File('a771f854-c2cb-408a-8c36-71af77811f3b') f.create_remote_copy(target='tgt', effects='resize/1x1/') request.assert_called_with('POST', 'files/', data={ "source": "a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/1x1/", "target": "tgt"}) # cdn url with no effects f = File('a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/2x2/') f.create_remote_copy(target='tgt') request.assert_called_with('POST', 'files/', data={ "source": "a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/2x2/", "target": "tgt"}) # cdn url with effects f = File('a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/3x3/') f.create_remote_copy(target='tgt', effects='flip/') request.assert_called_with('POST', 'files/', data={ "source": "a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/3x3/-/flip/", "target": "tgt"}) #cdn url with effects, set permissions to public f = File('a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/3x3/') f.create_remote_copy(target='tgt', effects='flip/', make_public=True) request.assert_called_with('POST', 'files/', data={ "source": "a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/3x3/-/flip/", "target": "tgt", "make_public": True}) #cdn url with effects, naming pattern f = File('a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/3x3/') f.create_remote_copy(target='tgt', effects='flip/', pattern='${uuid}') request.assert_called_with('POST', 'files/', data={ "source": "a771f854-c2cb-408a-8c36-71af77811f3b/-/resize/3x3/-/flip/", "target": "tgt", "pattern": "${uuid}"})