def test_init_with_url(self): url = 'https://www.filepicker.io/api/file/{}'.format(self.HANDLE) file = FilepickerFile(url=url) self.assertEqual(file.url, url) self.assertEqual(file.handle, self.HANDLE) self.assertEqual(file.metadata, {})
def get_signed_url(url): if url is None or url == '': return now = timezone.now() fpfile = FilepickerFile(url=url) # remove existing query params base_url = fpfile.url.split('?')[0] sig = UrlSignature.objects.filter( file_handle=fpfile.handle, expires__gte=now).order_by('-expires').first() if sig: return '{0}?policy={1}&signature={2}'.format(base_url, sig.policy, sig.signature) expiry = str(EXPIRY) json_policy = '{"handle":"%s","expiry":%s}' % (fpfile.handle, expiry) policy = base64.urlsafe_b64encode(json_policy) signature = hmac.new(settings.FILEPICKER_APP_SECRET, policy, hashlib.sha256).hexdigest() new_sig = UrlSignature(file_handle=fpfile.handle, policy=policy, signature=signature, expires=now + timezone.timedelta(0, EXPIRES_IN_SECONDS)) new_sig.save() return '{0}?policy={1}&signature={2}'.format(base_url, policy, signature)
def test_init_with_dict(self): file_dict = { 'url': "https://www.filepicker.io/api/file/" + self.HANDLE, 'size': 8811, 'type': 'image/jpg', 'filename': 'awesome.jpg' } file = FilepickerFile(response_dict=file_dict.copy()) self.assertEqual(file.url, file_dict['url']) self.assertEqual(file.handle, self.HANDLE) self.assertNotEqual(file.metadata, None) self.assertEqual(file.mimetype, file_dict['type']) self.assertRaises(AttributeError, file.__getattribute__, 'non_existent_attr')
def __post(self, storage, data=None, files=None, params=None): storage = storage or self.storage post_url = '{}/store/{}'.format(self.API_URL, storage) params['key'] = self.api_key response = requests.post(post_url, data=data, files=files, params=params, headers=self.HEADERS) try: response_dict = json.loads(response.text) return FilepickerFile(response_dict=response_dict, api_key=self.api_key, app_secret=self.app_secret, policies=self.policies) except ValueError: return response.raise_for_status()
def test_already_converted(self): f = FilepickerFile(url="filepicker.io/api/file/ZXC", temporary=True) self.assertEqual(f.convert(w=10), 'File already converted')
def setUp(self): self.file = FilepickerFile(handle=self.HANDLE)