def _test_upload_file(self): gds = GoogleDriveStorage() file_name = "{0}{1}{2}".format(os.path.dirname(os.path.abspath(__file__)), os.path.sep, "../test/gdrive_logo.png") result = gds.save("/test4/gdrive_logo.png", open(file_name, 'rb')) pprint(result) self.assertIsNotNone(result, u'Unable to upload file to Google Drive')
def test_upload_big_file(self): gds = GoogleDriveStorage() file_name = "{0}{1}{2}".format(os.path.dirname(os.path.abspath(__file__)), os.path.sep, "../test/huge_file") with open(file_name, "wb") as out: out.truncate(1024 * 1024 * 20) result = gds.save("/test5/huge_file", open(file_name, 'rb')) pprint(result) self.assertIsNotNone(result, u'Unable to upload file to Google Drive') os.remove(file_name) time.sleep(10)
def test_permission_full_write(self): full_write_permission = GoogleDriveFilePermission(GoogleDrivePermissionRole.WRITER, GoogleDrivePermissionType.ANYONE) gds = GoogleDriveStorage(permissions=(full_write_permission,)) file_name = "{0}{1}{2}".format(os.path.dirname(os.path.abspath(__file__)), os.path.sep, "../test/gdrive_logo.png") result = gds.save("/test4/gdrive_logo.png", open(file_name, 'rb')) pprint(result) self.assertIsNotNone(result, u'Unable to upload file to Google Drive') f = gds.open(result, "rb") pprint(f) pprint(len(f)) self.assertIsNotNone(f, "Unable to load data from Google Drive") time.sleep(10)
def test_upload_big_file(self): gds = GoogleDriveStorage() file_name = "{0}{1}{2}".format( os.path.dirname(os.path.abspath(__file__)), os.path.sep, "../test/huge_file") with open(file_name, "wb") as out: out.truncate(1024 * 1024 * 20) result = gds.save("/test5/huge_file", open(file_name, 'rb')) pprint(result) self.assertIsNotNone(result, u'Unable to upload file to Google Drive') os.remove(file_name) time.sleep(10)
def test_multiple_permission(self): full_write_to_foo = GoogleDriveFilePermission( GoogleDrivePermissionRole.WRITER, GoogleDrivePermissionType.USER, "*****@*****.**") read_only_to_anyone = GoogleDriveFilePermission( GoogleDrivePermissionRole.READER, GoogleDrivePermissionType.ANYONE) gds = GoogleDriveStorage(permissions=( full_write_to_foo, read_only_to_anyone, )) file_name = "{0}{1}{2}".format( os.path.dirname(os.path.abspath(__file__)), os.path.sep, "../test/gdrive_logo.png") result = gds.save("/test4/gdrive_logo.png", open(file_name, 'rb')) pprint(result) self.assertIsNotNone(result, u'Unable to upload file to Google Drive') f = gds.open(result, "rb") pprint(f) pprint(len(f)) self.assertIsNotNone(f, "Unable to load data from Google Drive")
def update(request, form, actu_id): """ update actu """ try: actu = Actu.objects.get(pk=actu_id) except KeyError: pass else: form.errors change_field = False if form.instance.image != "": form_image = request.FILES['image'] if 'TRAVIS' not in os.environ: from gdstorage.storage import GoogleDriveStorage gd_storage = GoogleDriveStorage() # storage new and delete old image gd_storage.delete(actu.image.name) actu.image = gd_storage.save( "".join(["actus/", form_image.name]), form_image) actu.save() code_img = actu.image.url.split("/")[5] actu.display_url_img = "".join( ["https://drive.google.com/uc?id=", code_img]) else: from django.core.files.storage import default_storage new_image = default_storage.save( "".join(["actus/", form_image.name]), form_image) default_storage.delete(actu.image) actu.image = new_image change_field = True if actu.title != form.instance.title: # title actu.title = form.instance.title change_field = True if actu.text != form.instance.text: # text actu.text = form.instance.text change_field = True if change_field is True: actu.change_date = timezone.now() actu.author = request.user actu.save()
def upload_and_save_file_item_2(atributo, file, proyecto, fase, item): gd_storage = GoogleDriveStorage() path = f'/PROY-{proyecto.nombre}-ID{proyecto.id}_/FASE-{fase.nombre}-ID{fase.id}_/ITEM-{item}_/ATRIB-{atributo.plantilla.nombre}_/VERS{atributo.version.version}_/FILENAME-{file.name}' print(path) gd_storage.save(path, file) return gd_storage.url(path)
def upload_file(request): if request.method == 'POST': targetDir = "/var/www/fip/SlayFIP/temporary_uploads/" + request.user.username if not os.path.exists(targetDir): os.makedirs(targetDir) if 'ajax_call' in request.POST: fileName = request.POST[ 'fileName'] # you receive the file name as a separate post data fileSize = request.POST[ 'fileSize'] # you receive the file size as a separate post data fileId = request.POST[ 'fileId'] # you receive the file identifier as a separate post data index = request.POST['chunkIndex'] # the current file chunk index totalChunks = int(request.POST['chunkCount'] ) # the total number of chunks for this file file_chunk = request.FILES['fileBlob'] target_file = targetDir + "/" + fileName outfile = targetDir + "/" + fileName target_file = target_file + "_" + str(index) if (chunk_handler(file_chunk, target_file)): chunks = get_chunk_list(targetDir, fileName + "_") allChunksUploaded = len(chunks) == totalChunks if allChunksUploaded: combineChunks(chunks, outfile, cleanup=True) request.session['fileName'] = fileName return_values = { 'chunkIndex': index, 'initialPreviewConfig': { 'type': 'other', 'caption': fileName, 'key': fileId, 'fileId': fileId, 'size': fileSize, }, 'append': True } return StreamingHttpResponse(json.dumps(return_values)) if "google_drive_upload" in request.POST: if not request.session["fileName"]: return redirect("/catinfo/?error=Unable to find file to upload") fileName = request.session["fileName"] outfile = targetDir + "/" + fileName file_blob = ContentFile(open(outfile, 'rb').read()) storage = GoogleDriveStorage() path = storage.save('FIPlog/' + fileName, file_blob) cat = Cats.objects.get(id=request.POST["cat_name"]) foo = BloodWork( bloodname=request.POST["bloodname"], cat_name=cat, bloodwork_date=request.POST["bloodwork_date"], notes=request.POST["notes"], bloodwork=path, ) foo.save() # Cleanup Temp Files in User's upload folder for filename in os.listdir(targetDir): file_path = os.path.join(targetDir, filename) try: if os.path.isfile(file_path) or os.path.islink(file_path): os.unlink(file_path) except Exception as e: print('Failed to delete %s. Reason: %s' % (file_path, e)) return redirect("/catinfo/?message=success&CatID=" + request.POST["cat_name"]) else: form = BloodWorkForm() return redirect('/catinfo')