def get_picture(request, pic_id): try: file_rcv = StorageBucket.read_file_from_storage(pic_id) except IOError: file_rcv = StorageBucket.read_file_from_storage( 'defaultCollection.png') return HttpResponse(file_rcv, content_type='image/png')
def profile_update(request): if request.method == 'POST': user = CustomUser.objects.get(username=request.user.username) user.first_name = request.POST.get('first_name', 'none') user.last_name = request.POST.get('last_name', 'none') user.save() ##profile_pic = request.POST.get('profile-pic', False) ##print(request.POST.get('profile-pic', False)) if request.FILES['profile-pic']: pic_file_id = get_user_profile_pic_id( CustomUser.objects.get(username=request.user.username)) StorageBucket.write_file_to_storage( pic_file_id, request.FILES['profile-pic'].read()) return messenger(request, message="Changes Saved Successfully!", m_type="success", url_return="/profile/") else: content = { # TODO: get real profile pic name after it gets implemented 'profile_pic': '/static/defaultprofile1.png', } return render(request, "profile/editUserProfile.html", content)
def download_sound(request, sound_id): meta_obj = metadata.objects.get(fileID=sound_id) storage = StorageBucket(meta_obj) storage.s_read_file_from_bucket() file_rcv = storage.file response = HttpResponse(file_rcv, content_type='audio/mpeg') response['Content-Disposition'] = 'attachment; filename= "{}"'.format( sound_id) return response
def get_profile_pic(request): pic_file_name = get_user_profile_pic_id( CustomUser.objects.get(username=request.user.username)) print("serving the file " + pic_file_name) try: file_rcv = StorageBucket.read_file_from_storage(pic_file_name) print("There is a profile picture") except IOError: file_rcv = StorageBucket.read_file_from_storage("defaultprofile1.png") print("There is NOT a profile picture") return HttpResponse(file_rcv, content_type='application/force-download')
def test_storage_secure_write_read(self): meta_obj = metadata.objects.get(user_id=self.user_id) storage_bucket = StorageBucket(meta_obj) storage_bucket.file = self.file storage_bucket.s_write_file_to_bucket() del storage_bucket storage_bucket2 = StorageBucket(meta_obj) storage_bucket2.s_read_file_from_bucket() file_rcv = storage_bucket2.file self.assertEqual(self.file, file_rcv, "The file properly saved in Storage")
def test_storage_read_write(self): name = "TestFile2.file" StorageBucket.write_file_to_storage(name, self.file) file_rcv = StorageBucket.read_file_from_storage(name) self.assertEqual(self.file, file_rcv)
def get_sound(request, sound_id): meta_obj = metadata.objects.get(fileID=sound_id) storage = StorageBucket(meta_obj) storage.s_read_file_from_bucket() file_rcv = storage.file return HttpResponse(file_rcv, content_type='application/force-download')
def upload(request): print(request.POST) print(request.FILES) print(request.user.user_id) title = request.POST.get('title', 'none') collection = request.POST.get('collection', 'none') category = request.POST.get('category', 'none') tags = request.POST.get('tags', 'none') length = request.POST.get('length', 'none') user = request.user.user_id fileID = hash_object = hashlib.md5(str.encode(title + user + collection)).hexdigest() file = metadata(user_id=user, title=title, rec_length=length, collection=collection, category=category, tags=tags, fileID=fileID) file.save() meta_obj = metadata.objects.get(fileID=fileID) storage_bucket = StorageBucket(meta_obj) storage_bucket.file = request.FILES['blob'].read() storage_bucket.s_write_file_to_bucket() del storage_bucket storage_bucket2 = StorageBucket(meta_obj) storage_bucket2.s_read_file_from_bucket() file_rcv = storage_bucket2.file collections = Collection.objects.all().filter(user_id=user) col_name = request.POST.get('collection', 'none') names = [collection.name for collection in collections] if col_name not in names: pic_id = hashlib.md5(str.encode(col_name + user)).hexdigest() c = Collection(user_id=user, name=request.POST.get('collection', 'none'), pic_id=pic_id) c.save() collection_pic = request.FILES.get('collection-pic', None) if collection_pic is not None: collection_pic.name = pic_id StorageBucket.write_file_to_storage(pic_id, collection_pic) return HttpResponse(fileID)