def test_create_key(self, mock_uuid): uid = 'e91dfc72-a19f-4da8-bdb1-70230cf35ab6' mock_uuid.return_value = uid user_hash = hash_string(str(self.user_test.id)) # digitalwork - anonymous digitalwork_key = File.create_key("digitalwork", "test.png", None) self.assertEqual(digitalwork_key, 'local/anonymous/digitalwork/test-{}.png'.format(uid)) # digitalwork digitalwork_key = File.create_key("digitalwork", "test.png", self.user_test) self.assertEqual( digitalwork_key, 'local/' + user_hash + '/digitalwork/test-{}.png'.format(uid)) # digitalwork key exists digitalwork_key = File.create_key("digitalwork", FIX_KEY_PNG, self.user_test) self.assertEqual( digitalwork_key, 'local/' + user_hash + '/digitalwork/ascribe_spiral-{}.png'.format(uid)) # thumbnail - anonymous thumbnail_key = File.create_key("thumbnail", "test.png", None) self.assertEqual(thumbnail_key, 'local/anonymous/thumbnail/test-{}.png'.format(uid)) # thumbnail thumbnail_key = File.create_key("thumbnail", FIX_KEY_PNG, self.user_test) self.assertEqual( thumbnail_key, 'local/' + user_hash + '/thumbnail/ascribe_spiral-{}.png'.format(uid))
def digital_work(alice, s3_bucket): from blobs.models import DigitalWork, File key = File.create_key('digitalwork', 'alice_work.gif', user=alice) k = Key(s3_bucket) k.key = key k.set_contents_from_string('abc') return DigitalWork.objects.create(user=alice, digital_work_file=key)
def _createFiles(request, data): if hasattr(request.auth, 'token'): # API User: needs to generate digital work first from url url = data['digital_work_key'] filename = os.path.split(url)[1] key = File.create_key("/".join([filename, "digitalwork"]), filename, request.user) digital_work = DigitalWork(user=request.user, digital_work_file=key) digital_work.save() digital_work.upload(url) else: # WEB User: file created by fineuploader digital_work = DigitalWork.objects.filter( user=request.user, digital_work_file=data['digital_work_key']).order_by('-id')[0] if 'thumbnail_file' in data and data['thumbnail_file']: # Thumbnail already provided thumbnail = data['thumbnail_file'] else: # Create thumbnail from digital_work thumbnail = digital_work.create_thumbnail() if thumbnail is None: # Thumbnail fallback thumbnail = Thumbnail(user=request.user, thumbnail_file=settings.THUMBNAIL_DEFAULT) thumbnail.save() data['digital_work'] = digital_work data['thumbnail'] = thumbnail return data
def test_create_unique_key_with_uuid(self, mock_uuid): uid = 'e91dfc72-a19f-4da8-bdb1-70230cf35ab6' mock_uuid.return_value = uid digitalwork_key = File.create_key('digital_work', 'test.png') self.assertEqual( digitalwork_key, 'local/anonymous/digital_work/test-e91dfc72-a19f-4da8-bdb1-70230cf35ab6.png' ) self.assertEqual(mock_uuid.call_count, 1)
def handle_uploaded_file(f, application, applicationkey): max_size = 2 * 1024 * 2044 print('applicaton=' + application + ' key=' + str(applicationkey)) print('Size ' + str(f.size)) print('Name ' + f.name) print('Content-type ' + f.content_type) if (f.size > max_size): return None bytearr = f.read() print(type(bytearr)) print(len(bytearr)) h1 = sha256() h1.update(bytearr) blob_sha256 = h1.hexdigest() print(blob_sha256) b = Blob(content=bytearr, sha256=blob_sha256) try: b.save() print('primary key=' + str(b.id)) except IntegrityError: try: b = Blob.objects.only('id').get(sha256=blob_sha256) print('primary key=' + str(b.id)) except Blob.DoesNotExist: print("Can't store, can't load - badly broken") return None # We have a blob, time to add the file entry f = File(sha256=blob_sha256, application=application, applicationkey=applicationkey, blob=b, contenttype=f.content_type, name=f.name) f.save() try: print('file primary key=' + str(f.id)) return f except IntegrityError: print('file fail')
def updateMonitorEncoding(self, job): """ Fetch the status of the Zencoder job and finalize :param job: JOB_CONVERTVIDEO - bitcoin_id carries the zencoder job_id """ if type(job) is int: job = JobMonitor.objects.get(id=job, description=settings.JOB_CONVERTVIDEO) zencode_job_id = int(job.resource_id) job_str = 'Job ' + str(job.id) percent_done, state = zencodeProgress(zencode_job_id) job.percent_done = percent_done job.save() details = zencodeJobDetails(zencode_job_id) try: assert (state == 'finished') urls = [f['url'] for f in details['job']['output_media_files']] # Set thumbnail to frame if not chosen piece = Piece.objects.filter(digital_work_id=job.object_id)[0] thumbnail = piece.thumbnail if thumbnail.key == settings.THUMBNAIL_DEFAULT: thumb_keys = { k: piece.digital_work.associated_key('thumbnail', k + '/frame_0000.png') for k, v in settings.THUMBNAIL_SIZES.iteritems()} thumbnail.thumbnail_file = thumb_keys[settings.THUMBNAIL_SIZE_DEFAULT] thumb_urls = { k: File.url_safe_from_key(v) for k, v in thumb_keys.iteritems()} thumbnail.thumbnail_sizes = thumb_urls thumbnail.save() print thumbnail.key s = job_str + ': Video conversion completed and uploaded to:' for url in urls: s += str("\n\t-" + url) print s except AssertionError as e: # job is converted but needs to be uploaded... if job.percent_done == 100: job.percent_done = 99 job.save() # see https://app.zencoder.com/docs/api/jobs/show print job_str + ': Video is converting on Zencoder - ' + str(int(job.percent_done)) + '%' self.retry(exc=e, countdown=15)
def create_key(request): body = json.loads(request.body) filename = body['filename'] _, extension = os.path.splitext(filename) uuid = body['uuid'] + extension if 'uuid' in body else filename # TODO handle key error - use serializer perhaps category = body['category'] if category == 'digitalwork': category = '/'.join([body['uuid'], category]) elif 'piece_id' in body: category = DigitalWork.piece_to_category(body['piece_id'], category) return Response({'key': File.create_key(category, uuid, request.user)}, content_type='application/json')
def test_create_non_unique_key(self): digitalwork_key = File.create_key('digitalwork', 'test.png', unique=False) self.assertEqual(digitalwork_key, 'local/anonymous/digitalwork/test.png')