class ExhibitStorage(FileSystemStorage): """ Assume that incoming paths are of the form <username>/.../... """ def __init__(self, *args, **kwargs): self.__userdata_storage = FileSystemStorage(location=kwargs['location'], base_url=kwargs['base_url']) def _open(self, name, mode='rb'): return self.__userdata_storage._open(chunk_path(name) + name, mode) def _save(self, name, content): chunk = chunk_path(name) fullname = chunk + name if (self.__userdata_storage.exists(fullname)): self.__userdata_storage.delete(fullname) result = self.__userdata_storage._save(fullname, content) return result.partition(chunk)[2] def exists(self, name): return self.__userdata_storage.exists(chunk_path(name) + name) def path(self, name): return self.__userdata_storage.path(chunk_path(name) + name) def size(self, name): return self.__userdata_storage.size(chunk_path(name) + name) def delete(self, name): return self.__userdata_storage.delete(chunk_path(name) + name) def url(self, name): return self.__userdata_storage.url(name) def get_available_name(self, name): return self.__userdata_storage.get_available_n
def createNewModel(self, modelName): """Creates a new model """ try: storage = FileSystemStorage( os.path.join(settings.MEDIA_ROOT, 'models')) company_code = self.client_session.company_code user_full_path = f'{storage.base_location}/' folderSufix = 1 new_model_name = modelName while storage.exists(f'{user_full_path}{new_model_name}/'): folderSufix += 1 new_model_name = f'{modelName}_{folderSufix}' folder_path = f'{user_full_path}{new_model_name}/' model_file = f'{folder_path}{new_model_name}.ppl' if not storage.exists(folder_path): os.mkdir(folder_path) calcEngine = CalcEngine.factory(self.client_session) if calcEngine.createNewModel(model_file, new_model_name): self.closeModel() return self.openModel( os.path.join(user_full_path, new_model_name, new_model_name + ".ppl")) except Exception as ex: raise ex
def createNewModel(self, modelName): """Creates a new model """ try: storage = FileSystemStorage(join(settings.MEDIA_ROOT, 'models')) folderSufix = 1 new_model_name = modelName while storage.exists(join(storage.base_location, new_model_name)): folderSufix += 1 new_model_name = f'{modelName}_{folderSufix}' folder_path = join(storage.base_location, new_model_name) model_file = join(folder_path, f'{new_model_name}.ppl') if not storage.exists(folder_path): os.mkdir(folder_path) calcEngine = CalcEngine.factory(self.client_session) if calcEngine.createNewModel(model_file, new_model_name): self.closeModel() return self.openModel( join(storage.base_location, new_model_name, f'{new_model_name}.ppl')) except Exception as ex: raise ex
def simple_upload(request): if request.method == 'POST' and request.FILES['d']: tables = request.FILES['t'] datas = request.FILES['d'] fs = FileSystemStorage() tablesName = fs.save(tables.name, tables) datasName = fs.save(datas.name, datas) if (fs.exists("mydb")): fs.delete("mydb") with open(fs.path(tablesName), "r") as inputTableJSON: for table in json.load(inputTableJSON): createTableWith(table['name'], table['schemas'], fs.base_location + "/mydb") if fs.exists('mydb'): fs.delete(tablesName) with open(fs.path(datasName), "r") as inputJSON: data = inputJSON.read() insertDataInTable(data, fs.path('mydb')) fs.delete(datasName) uploaded_file_url = fs.url('mydb') return render(request, 'jsonsqlite/simple_upload.html', {'uploaded_file_url': uploaded_file_url}) return render(request, 'jsonsqlite/simple_upload.html')
def development_tracker_response(request): if request.method == "POST" and len(request.FILES) != 0: if request.FILES['file1']: myfile = request.FILES['file1'] fs = FileSystemStorage() if fs.exists(request.user.username + "1.png"): fs.delete(request.user.username + "1.png") filename = fs.save(request.user.username + "1.png", myfile) uploaded_file_url = fs.url(filename) if myfile.name[len(myfile.name)-3:len(myfile.name)] != "png": return render(request, 'label/qgis_support_app.html',{'is_not_file_valid':True}) if request.FILES['file2']: myfile = request.FILES['file2'] fs = FileSystemStorage() if fs.exists(request.user.username + "2.png"): fs.delete(request.user.username + "2.png") filename = fs.save(request.user.username + "2.png", myfile) uploaded_file_url = fs.url(filename) if myfile.name[len(myfile.name)-3:len(myfile.name)] != "png": return render(request, 'label/development_tracker_app.html',{'is_not_file_valid':True}) if(request.POST.get('type') == "road"): ps.save_image(Unet_model_Road, graph_road, os.path.join(settings.MEDIA_ROOT, request.user.username + "1.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "1_mask.png")) ps.save_image(Unet_model_Road, graph_road, os.path.join(settings.MEDIA_ROOT, request.user.username + "2.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "2_mask.png")) difference = ps.difference_analyzier(os.path.join(settings.MEDIA_ROOT, request.user.username + "1_mask.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "2_mask.png")) if(request.POST.get('type') == "building"): ps.save_image(Unet_model_Building, graph_building, os.path.join(settings.MEDIA_ROOT, request.user.username + "1.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "1_mask.png")) ps.save_image(Unet_model_Building, graph_building, os.path.join(settings.MEDIA_ROOT, request.user.username + "2.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "2_mask.png")) difference = ps.difference_analyzier(os.path.join(settings.MEDIA_ROOT, request.user.username + "1_mask.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "2_mask.png")) if(request.POST.get('type') == "car"): ps.save_image(Unet_model_Car, graph_car, os.path.join(settings.MEDIA_ROOT, request.user.username + "1.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "1_mask.png")) ps.save_image(Unet_model_Car, graph_car, os.path.join(settings.MEDIA_ROOT, request.user.username + "2.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "2_mask.png")) difference = ps.difference_analyzier(os.path.join(settings.MEDIA_ROOT, request.user.username + "1_mask.png"), os.path.join(settings.MEDIA_ROOT, request.user.username + "2_mask.png")) return render(request, "label/development_tracker_response.html", {'image_url1': "/media/" + request.user.username + "1_mask.png", 'image_url2': "/media/" + request.user.username + "2_mask.png", 'difference': difference}) return redirect(request.META.get('HTTP_REFERER'))
def post(self, request, format=None): arquivo = request.data.get('arquivo0') if request.data.get('getInformation') == 'true': data = getInformations(arquivo) return JsonResponse(data) else: modo = int(request.data.get('modo')) onlyOne = request.data.get('onlyOne') # modo 0: Por intervalo if modo == 0: start = int(request.data.get('start')) - 1 end = int(request.data.get('end')) IntervalSplit(arquivo, start, end, onlyOne) # modo 1: Por tamanho elif modo == 1: size = int(request.data.get('size')) * 1000 SizeSplit(arquivo, size) # modo 2: Selecionar páginas else: extractType = request.data.get('tipoExtracao') if extractType == 'select': pages = request.data.get('pages') SelectSplit(arquivo, pages) else: SplitAll(arquivo) fs = FileSystemStorage() # Enviando como PDF if onlyOne == 'true': filename = os.path.join( os.path.join(os.path.dirname(os.path.abspath(__package__)), 'documentosDividos'), "document-output.pdf") if fs.exists(filename): with fs.open(filename) as pdf: response = HttpResponse(pdf, content_type='application/pdf') response[ 'Content-Disposition'] = 'attachment; filename="mypdf.pdf"' return response # Enviando como zip else: shutil.make_archive("output_filename", 'zip', 'documentosDividos') # Buscando o arquivo que foi criado filename = os.path.join( os.path.dirname(os.path.abspath(__package__)), 'output_filename.zip') if fs.exists(filename): with fs.open(filename) as pdf: response = HttpResponse( pdf, content_type='application/octet-stream') response[ 'Content-Disposition'] = 'attachment; filename=my_file.zip' return response
def logs(request): context = {} if request.method == 'POST': try: ivr_file = request.FILES['ivr'] mobile_file = request.FILES['mobile'] fs = FileSystemStorage() if fs.exists('ivr_file.csv'): fs.delete('ivr_file.csv') if fs.exists('mobile.csv'): fs.delete('mobile.csv') fs.save('ivr_file.csv', ivr_file) fs.save('mobile.csv', mobile_file) path_ivr = settings.BASE_DIR / 'media/ivr_file.csv' path_mobile = settings.BASE_DIR / 'media/mobile.csv' path_response = settings.BASE_DIR / 'media/response.csv' dump_redundancy_set = main(path_ivr, path_mobile) with open(path_ivr) as csv_ivr, open(path_response, 'w') as response: csv_ivr_file = csv.reader(csv_ivr) next(csv_ivr_file) csv_response = csv.writer(response) csv_response.writerow([ 'Mobile_Number', 'Status', 'Duration', 'Department (if any)', 'Time' ]) redundancy_set = set({}) for ivr_file_entry in csv_ivr_file: if ivr_file_entry[4] in dump_redundancy_set: write(ivr_file_entry, redundancy_set, csv_response) context['result'] = 'success' except Exception as e: context['exception'] = e print(e) return render(request, 'index.html', context)
def extract_sub(course, assign, user): #hemant fs = FileSystemStorage() user_file_path = course + '/' + assign + '/' + user if fs.exists(user_file_path): if len(fs.listdir(user_file_path)[1]) > 0: file_name = fs.listdir(user_file_path)[1][0] if not fs.exists(user_file_path + '/' + file_name.rstrip('.zip')): zipf = zipfile.ZipFile(settings.MEDIA_ROOT + '/' + user_file_path + '/' + file_name) zipf.extractall(settings.MEDIA_ROOT + '/' + user_file_path)
def assertMediaCleanedUp(instance, *files_storenames, msg_case_not_created=None, msg_case_not_deleted=None): storage = FileSystemStorage() assert all(storage.exists(file) for file in files_storenames), \ msg_case_not_created or 'Files must be created and still exist' instance.delete() assert all(not storage.exists(file) for file in files_storenames), \ msg_case_not_deleted or \ 'Media dir must be cleaned up after ' \ f'deletion of the {instance._meta.verbose_name}'
def analysis_metaupload(request): ano = request.POST['image_a_no'] sno = request.POST['image_step_no'] type = request.POST['image_type'] data = {} if request.method == "POST": uploaded_file = request.FILES['ufile'] name_old = uploaded_file.name name_ext = os.path.splitext(name_old)[1] name_new = 'tdata_' + str(sno) + '_' + type # print(name_new) file_name = name_new + name_ext #fs = FileSystemStorage() fs = FileSystemStorage(location='static/datafiles') if (fs.exists(file_name)): fs.delete(file_name) name = fs.save(file_name, uploaded_file) data['url'] = fs.url(name) data['result_msg'] = 'Data file uploaded...' else: data['result_msg'] = 'Data file upload error...' return redirect("/analysis_upload?a_no=" + ano + "&step_no=" + sno)
def TrainView(request): form = trainForm() if request.method == "POST": form = trainForm(request.POST, request.FILES) if form.is_valid(): model_name = form.cleaned_data['model_name'] data_file = request.FILES['data'] print("uploaded files", data_file.name) train_on = form.cleaned_data['train_on'] fs = FileSystemStorage() if fs.exists('data/' + data_file.name): os.remove(os.path.join("media/data", data_file.name)) fs.save('data/' + data_file.name, data_file) print(train_on) if train_on == 'new_model': start_training(data_file.name, model_name) else: train_existing(data_file.name, model_name, train_on) hsvm = TrainModel() hsvm.data_name = data_file.name hsvm.model_name = model_name + '.model' hsvm.save() form = trainForm() return render(request, 'train.html', {'form': form}) return render(request, 'train.html', {'form': form})
def uploadFile(request): if 'user' in request.session: if request.method == 'POST' and request.FILES['myfile']: nRoute = request.POST['dir'] myfile = request.FILES['myfile'] try: nRoute = checkRoute(request.POST['dir']) base_dir = request.session['user']['directory'] fs = FileSystemStorage(location=os.path.join(base_dir, nRoute)) if not fs.exists(myfile.name): fs.save(myfile.name, myfile) request.session['uploaded'] = True return HttpResponseRedirect('/cloud/userPage') else: request.session['uploaded'] = False return HttpResponseRedirect('/cloud/userPage') except ValueError as e: return HttpResponse('Nice try') else: return HttpResponse('Choose a file to upload by POST') else: return HttpResponse('Log in at /cloud')
def get_images(request, image_name=""): jwtToken = request.META['HTTP_AUTHORIZATION'] images = [] image = "" error = "False" try: payload = jwt.decode(jwtToken, 'bhandari', True) if image_name: fs = FileSystemStorage() if fs.exists(jwtToken + image_name): image = fs.url(jwtToken + image_name) else: image = "image not found" else: fs = FileSystemStorage() files = fs.listdir('') if not files: error="NO images to get" for f in files: for img in f: if jwtToken in img: images.append("/media/" + img) except jwt.DecodeError: error = "Decoding Error" except jwt.ExpiredSignature: error = "Signature Expired" except jwt.InvalidTokenError: error = "Invalid Token" return HttpResponse(json.dumps({"image": image, "images": images, "error": error}))
def file_upload(request): is_business = False user_id = request.data.get("userId") caption = request.data.get("caption") file = request.FILES["file"] file_name = '' fs = FileSystemStorage() if fs.exists(file.name): name = fs.get_available_name(file.name) fs.save(name, file) file_name = name else: fs.save(file.name, file) file_name = file.name if file.name.endswith('.mp4'): img_data = base64.b64decode(request.data.get("thumb")) new_filename = 'media/thumbnails/' + file_name.replace("mp4", "png") with open(new_filename, 'wb') as f: f.write(img_data) UserDetail.objects.get(user_id=user_id) if UserDetail.isBusiness: is_business = True p = Post(user_id=user_id, caption=caption, content=file_name, likes=0, comments=0, date=datetime.datetime.now().timestamp(), isBusiness=is_business) p.save() trending = Trending(point=0, post_id=p.id) trending.save() return Response(status=HTTP_200_OK)
def rm_mem_from_proj(request): if request.method == "PUT": try: res = QueryDict(request.body) proj = Project.objects.get(id=res.get("project_id")) for member in res.get("users_arr").split(","): rm_proj_user_ws = proj.workspace_set.filter(user=member) for ws in rm_proj_user_ws: fs = FileSystemStorage() if fs.exists(ws.file_path): for file in fs.listdir(ws.file_path)[1]: fs.delete(ws.file_path + file) ws.delete() proj.members.remove( Member.objects.get(usr=User.objects.get(username=member))) LogMessage(log=proj.log, log_type="remove_member", user=res.get("username"), timestamp=timezone.now(), body=res.get("username") + " removed " + member + " from the project").save() return HttpResponse( reverse('projectlab:view_project', args=( res.get("username"), proj.id, ))) except ObjectDoesNotExist: return render(request, 'projectlab/403.html', {'user': user}) except: return HttpResponseServerError( "Error updating project. Please try again.")
def handle(self, *args, **options): app_label = options['app_label'] model = options['model'] excluded_fields = options['excluded_fields'] replace_existing = options['replace_existing'] use_placeholder = options['use_placeholder'] model_class = self._get_model(app_label, model) verbose_name = force_text(model_class._meta.verbose_name) output_path, file_name = self._get_location(options, verbose_name, model) if not os.path.exists(output_path): os.makedirs(output_path, exist_ok=True) fs = FileSystemStorage(location=output_path) if fs.exists(file_name): if replace_existing: fs.delete(file_name) else: self.stdout.write( self.style.WARNING( f'File "{file_name}" already exists a new component file will be created.', ), ) output = self._render_js( fs, file_name, self._get_context(model_class, model, excluded_fields, use_placeholder), ) self.stdout.write( self.style.SUCCESS( f'Successfully created component file "{output}": \n{fs.path(output)}', ), )
def ensureUserWorkspace(self): storage = FileSystemStorage(os.path.join(settings.MEDIA_ROOT, 'models')) # User Workspace if not storage.exists(storage.base_location): os.makedirs(storage.base_location)
def submit_assign_mass(request, course, announce): #mod by hemant if request.user.is_authenticated: if request.method == 'POST': upload_file = request.FILES['document'] with zipfile.ZipFile(upload_file, 'r') as f: for name in f.namelist(): data = f.read(name) if len(name.split('/')) == 3 and not (name.split('/')[2] == ''): fs = FileSystemStorage() k = name.split('/') tail = k[1] + '/' + k[2] file_path = course + '/' + announce + '/' + k[1] if fs.exists(file_path) and ( fs.listdir(file_path)[1]) != 0: for fil in fs.listdir(file_path)[1]: fs.delete(file_path + '/' + fil) fs.save(course + '/' + announce + '/' + tail, io.BytesIO(data)) return render(request, 'files/upload.html', { 'course': course, 'announce': announce }) else: return redirect('account/login')
def post(self, request, format=None): arquivos = [] for index in range(len(request.data)): aux = 'arquivo' + str(index) arquivos.append(request.data.get(aux)) modoExtracao = request.data.get('tipoExtracao') pagina = int(request.data.get('pages')) output = ExtracTables(arquivos, modoExtracao, pagina) fs = FileSystemStorage() # filename = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__package__)), 'documentosDividos'), "document-output.pdf") # path = os.path.abspath(__file__).replace('pdftoxml.py','') path = os.path.abspath(__package__).replace('linaPDF', '') file = 'data1.xlsx' # file = 'document-output.pdf' filename = os.path.join(path, file) if fs.exists(filename): with fs.open(filename) as excel: response = HttpResponse( excel, content_type= 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) response[ 'Content-Disposition'] = 'attachment; filename="mypdf.xlsx"' return response
def get_user_sub_name(course, assign, user): #hemant fs = FileSystemStorage() user_file_path = course + '/' + assign + '/' + user if fs.exists(user_file_path) and len(fs.listdir(user_file_path)[1]) > 0: file_name = fs.listdir(user_file_path)[1][0] return file_name.rstrip('.zip') return ''
def get_assign(request, course, announce): #mod by hemant if request.user.is_authenticated: if request.method == 'POST': s = getzip(course, announce) resp = HttpResponse(s.getvalue(), content_type='application/x-zip-compressed') resp['Content-Disposition'] = 'attachment; filename=%s' % ( course + '-' + announce + '.zip') return resp fs = FileSystemStorage() sublist = [] subpath = [] if fs.exists(course + '/' + announce): dirs = fs.listdir(course + '/' + announce) dirs = dirs[0] dirs.remove('__grades') sublist = dirs for x in dirs: sub_name = fs.listdir(course + '/' + announce + '/' + x)[1][0] subpath.append('/media/' + course + '/' + announce + '/' + x + '/' + sub_name) return render( request, 'files/download.html', { 'course': course, 'announce': announce, 'sublist': list(zip(sublist, subpath)) }) else: return redirect('account/login')
def actualizador_de_gestion(request): user_instance = User.objects.get(username=request.user.username) persona_instance = Persona.objects.get( codigo_persona_beme=user_instance.username) permisos = permisos_de_grupo(user_instance) upload_file_form = UploadFileForm(persona=persona_instance) if request.method == 'POST': gestor = Persona.objects.get( codigo_persona_beme=request.POST['gestor']) upload_file = request.FILES['info_gestion'] fs = FileSystemStorage() path = os.path.join(BASE_DIR, 'excel_files', 'gestiones_realizadas', gestor.zona, gestor.modulo, today_srt, gestor.codigo_persona_beme, upload_file.name) if fs.exists(path): fs.delete(path) fs.save(name=path, content=upload_file) else: fs.save(name=path, content=upload_file) uplodad_info_to_database(path, gestor) return render(request=request, template_name='personas_beme/actualizador_de_gestion.html', context={ 'upload_file_form': upload_file_form, 'user_instance': user_instance, 'permisos': permisos, 'persona_instance': persona_instance })
def emailviewattachment(request, pk): BASE_DIR = settings.BASE_DIR cursor3 = connection.cursor() cursor3.execute( "SELECT " "`Doc_detailsid_tblDoc_details`, " "attachmentname_tbldocdetails " "FROM quotation_tbldoc_details " "WHERE Doc_detailsid_tblDoc_details=%s ", [pk]) docdetails = cursor3.fetchall() for x in docdetails: docdetailsid = x[0] attachmentname = x[1] fs = FileSystemStorage() storedfilename = BASE_DIR + '/emailattachments/' + str( docdetailsid) + '.pdf' if fs.exists(storedfilename): with fs.open(storedfilename) as pdf: response = HttpResponse(pdf, content_type='application/pdf') response['Content-Disposition'] = 'inline; filename="output.pdf"' return response else: return HttpResponseNotFound( "The requested pdf was not found in our server.")
class LocallyMirroredS3BotoStorage(S3BotoStorage): def __init__(self, *args, **kwargs): super(LocallyMirroredS3BotoStorage, self).__init__(*args, **kwargs) self.mirror = FileSystemStorage(location=settings.S3_MIRROR_ROOT) def delete(self, name): super(LocallyMirroredS3BotoStorage, self).delete(name) try: self.mirror.delete(name) except OSError: full_path = self.mirror.path(name) if os.path.exists(full_path): os.rmdir(full_path) def exists(self, name): exists_local = self.mirror.exists(name) if exists_local: return True else: exists_remote = super(LocallyMirroredS3BotoStorage, self).exists(name) if exists_remote: self.mirror._save(name, ContentFile("")) return True return False def _save(self, name, content): cleaned_name = super(LocallyMirroredS3BotoStorage, self)._save(name, content) self.mirror._save(name, ContentFile("")) return cleaned_name
def result(request): category = str(request.session['data']) inner_category = str(request.session['inner_category']) print 'Category received is:' + inner_category #do the actual conversion here. email = get_mail(category, inner_category) if request.method == 'POST': mail_obj = Email() mail_obj.author = 'Anon' mail_obj.mail_type = category.title() mail_obj.category = inner_category mail_obj.content = email mail_obj.save() get_pdf(email) fs = FileSystemStorage() filename = 'write_final.pdf' if fs.exists(filename): with fs.open(filename) as pdf: response = HttpResponse(pdf, content_type='application/pdf') response[ 'Content-Disposition'] = 'attachment; filename="write_final.pdf"' return response else: return HttpResponseNotFound( 'The requested pdf was not found in our server.') return render(request, 'emails/result.html', {'email': email})
def upload_file(request): myfile = request.FILES['file'] fs = FileSystemStorage(location='file') if fs.exists(myfile.name): fs.delete(myfile.name) fs.save(myfile.name, myfile) return JsonResponse({**error_code.CLACK_SUCCESS})
def delete_file(self, request, **kwargs): """ Delete a file from session\n Params: - filename: str """ session_id = self.get_object().pk file_name = request.POST.get('filename') if not file_name: raise APIException('Provide a file name.') try: storage = FileSystemStorage(location=os.path.join( settings.MEDIA_ROOT, settings.FILEBROWSER_DOCUMENTS_DIRECTORY, session_id)) if storage.exists(file_name): storage.delete(file_name) file_tasks = Task.objects \ .filter(metadata__session_id=session_id) \ .filter(metadata__file_name=file_name) for file_task in file_tasks: if file_task.metadata.get('file_name') == file_name: purge_task(file_task.id) Document.objects \ .filter(upload_session_id=session_id, name=file_name) \ .delete() return Response('Deleted') raise APIException("File doesn't exist") except Exception as e: raise APIException(str(e))
def patch(request, image_name): error = "False" uploaded_file_url = "" jwtToken = request.META['HTTP_AUTHORIZATION'] try: payload = jwt.decode(jwtToken, 'bhandari', True) if request.method == 'POST' and request.FILES['image_file']: fs = FileSystemStorage() if fs.exists(jwtToken + image_name): fs.delete(jwtToken + image_name) image_file = request.FILES['image_file'] extension = os.path.splitext(image_file.name)[1] if not ( extension == '.jpg' or extension == '.png' or extension == '.gif' or extension == '.jpeg' or extension == '.bmp'): error = "not an image file" fs = FileSystemStorage() filename = fs.save(jwtToken + image_file.name, image_file) uploaded_file_url = fs.url(filename) return HttpResponse(json.dumps({"image_url": uploaded_file_url, "error": error})) else: error = "Image Not Found" return HttpResponse(json.dumps({"image_url": uploaded_file_url, "error": error})) except jwt.ExpiredSignature: error = "Signature Expired" except jwt.DecodeError: error = "Decoding Error" except jwt.InvalidTokenError: error = "Invalid Token" return HttpResponse(json.dumps({"image_url": uploaded_file_url, "error": error}))
def unzip_microsite(self, zipfilepath): """ handles zips that are microsites, extract and dont import as content items, return a URL """ filepath, filename = os.path.split(zipfilepath) zipname, extension = os.path.splitext(filename) saved_dir = filepath fs = FileSystemStorage(location=saved_dir) if zipname: if fs.exists(zipname): zipname = fs.get_available_name(zipname) saved_dir = saved_dir + '/' + zipname os.makedirs(saved_dir) self.extract_zip(zipfilepath, saved_dir) #need to save both as if user overrides that we return to something in a lower directory we still need to track the root location self.storageLocation = getMediaURL( saved_dir) #saved_dir.replace(MEDIA_ROOT, MEDIA_URL) self.micrositeURL = self.storageLocation #saved_dir.replace(MEDIA_ROOT, MEDIA_URL) for fname in os.listdir(saved_dir): if fname.lower() == 'index.html' or fname.lower() == 'index.htm': self.micrositeURL = os.path.join(getMediaURL(saved_dir), fname) #Delete the zip try: os.remove(zipfilepath) except: logger.warn("Attempted file deletion failed for file: " + zipfilepath) else: logger.warn("Deleted file: " + zipfilepath)
def upload_handler(request, metadata={}): """ writes file(s) to appropriate spot on disk, collects metadata from the form, calls FileImporter on it """ from datetime import datetime timing_now = datetime.now() timing_string = timing_now.isoformat(' ') new_file = request.FILES['content_file'] # write the file to disk #save_dir = MEDIA_ROOT + UPLOAD_CONTENT_DIR # First check if there is enoght space internally and if not return the path the external HD, # the second check below is then a definative check for any free space save_dir = getSaveLocation(new_file.size, os.path.join(MEDIA_ROOT, UPLOAD_CONTENT_DIR)) if isThereEnoughSpace(new_file.size, save_dir): fs = FileSystemStorage(location=save_dir) if fs.exists(new_file.name): new_file.name = fs.get_available_name(new_file.name) content_file = fs.save(new_file.name, new_file) #return MEDIA_URL + UPLOAD_CONTENT_DIR + new_file.name return getMediaURL(save_dir) + new_file.name else: return None
def saveModelAs(self, modelName): """Saves Model With New Name""" if self.checkModelOpen(): currentPath = self.client_session.modelInfo.uri folderPath = currentPath[:currentPath.rfind('/') + 1] file_path = f'{folderPath}{modelName}.ppl' storage = FileSystemStorage( os.path.join(settings.MEDIA_ROOT, 'models')) if not storage.exists(file_path): calcEngine = CalcEngine.factory(self.client_session) try: fullPath = f'{storage.base_location}/{file_path}' newModel = calcEngine.saveModel(fullPath) current_session = self.getSession() res = current_session.modelInfo res.uri = file_path self.saveSession() calcEngine = None return res except Exception as ex: raise exceptions.ErrorDetail( f'Error when saving the model: {str(ex)}') raise exceptions.NotAcceptable( "The file name you provide already exists") raise exceptions.NotAcceptable("There's no currentModel")
def setUp(self): super(RrdTests, self).setUp() filesystem = FileSystemStorage(location=self.path) if not filesystem.exists('demo.rrd'): self._setup_rrd_file(self.path) print "[i] created demo.rrd architecture dependent dependency for "\ "unit tests based on %s/rrd.xml" % self.path
def get_html_code(self, context): file_storage = FileSystemStorage(location=settings.STATICFILES_DIRS[0], base_url=settings.STATIC_URL) if not file_storage.exists('downloads') or not file_storage.listdir('downloads')[0]: # note: listdir returns a tuple:- (directory, files). We wish to use folders as headings and the files in # the sub folders for links, if the download folder has only files without folders, we ignore them because # they will not be used return self.empty_html_message() return self.build_html(file_storage)
def save(self): fileStorage=FileSystemStorage(location=BASE_FOTOS) fileStorage.file_permissions_mode = 0644 if fileStorage.exists(self.fotos.name): raise ValidationError('El nombre o la foto ya existe.') elif self.pk is not None: foto_original = Fotos.objects.get(pk=self.pk) if foto_original.fotos != self.fotos: os.remove(os.path.join(settings.MEDIA_ROOT, foto_original.fotos.name)) super(Fotos, self).save() super(Fotos, self).save()
class FileSaveRaceConditionTest(TestCase): def setUp(self): self.storage_dir = tempfile.mkdtemp() self.storage = FileSystemStorage(self.storage_dir) self.thread = threading.Thread(target=self.save_file, args=['conflict']) def tearDown(self): shutil.rmtree(self.storage_dir) def save_file(self, name): name = self.storage.save(name, SlowFile("Data")) def test_race_condition(self): self.thread.start() name = self.save_file('conflict') self.thread.join() self.assert_(self.storage.exists('conflict')) self.assert_(self.storage.exists('conflict_')) self.storage.delete('conflict') self.storage.delete('conflict_')
def handle(self, *args, **options): location = self.get_location() file = 'reverse.js' fs = FileSystemStorage(location=location) if fs.exists(file): fs.delete(file) default_urlresolver = urlresolvers.get_resolver(None) content = generate_js(default_urlresolver) fs.save(file, ContentFile(content)) if len(sys.argv) > 1 and sys.argv[1] in ['collectstatic_js_reverse']: self.stdout.write('js-reverse file written to %s' % (location)) # pragma: no cover
def handle(self, *args, **options): if not hasattr(settings, 'STATIC_ROOT') or not settings.STATIC_ROOT: raise ImproperlyConfigured('The collectstatic_js_reverse command needs settings.STATIC_ROOT to be set.') location = os.path.join(settings.STATIC_ROOT, 'django_js_reverse', 'js') file = 'reverse.js' fs = FileSystemStorage(location=location) if fs.exists(file): fs.delete(file) content = urls_js() fs.save(file, ContentFile(content)) if len(sys.argv) > 1 and sys.argv[1] in ['collectstatic_js_reverse']: self.stdout.write('js-reverse file written to %s' % (location)) # pragma: no cover
def post_to_inbox(request,model_id): m=get_object_or_404(LearnedModel,id=model_id); uploaded_file=request.FILES['file']; fn=request.REQUEST['name']; original_name =uploaded_file.name loc=os.path.join(m.location,"inbox"); if not os.path.exists(loc): os.makedirs(loc) storage = FileSystemStorage(loc); tgt=os.path.join(loc,fn); if storage.exists(tgt): storage.delete(tgt); fname=storage.save(os.path.join(loc,fn),uploaded_file); return HttpResponse("+")
class DummyExternalStorage(Storage): def __init__(self, *args, **kwargs): self.wrapped = FileSystemStorage(*args, **kwargs) def path(self, name): # Overridden to give it the behaviour of the base Storage class # This is what an external storage backend would have raise NotImplementedError("This backend doesn't support absolute paths.") def _open(self, name, mode='rb'): # Overridden to return a DummyExternalStorageFile instead of a normal # File object return DummyExternalStorageFile(open(self.wrapped.path(name), mode)) # Wrap all other functions def _save(self, name, content): return self.wrapped._save(name, content) def delete(self, name): self.wrapped.delete(name) def exists(self, name): return self.wrapped.exists(name) def listdir(self, path): return self.wrapped.listdir(path) def size(self, name): return self.wrapped.size(name) def url(self, name): return self.wrapped.url(name) def accessed_time(self, name): return self.wrapped.accessed_time(name) def created_time(self, name): return self.wrapped.created_time(name) def modified_time(self, name): return self.wrapped.modified_time(name)
class RemoteFinder(BaseFinder): def __init__(self): self.always_verify = getattr(settings, "REMOTE_FINDER_ALWAYS_VERIFY", False) self.cache_dir = getattr(settings, "REMOTE_FINDER_CACHE_DIR", None) if not self.cache_dir: raise ImproperlyConfigured("settings.REMOTE_FINDER_CACHE_DIR must point to a cache directory.") self.storage = FileSystemStorage(self.cache_dir) try: resources_setting = settings.REMOTE_FINDER_RESOURCES except AttributeError: logger.warning("RemoteFinder is enabled, but settings.REMOTE_FINDER_RESOURCES is not defined.") resources_setting = () if not isinstance(resources_setting, (list, tuple)): raise ImproperlyConfigured("settings.REMOTE_FINDER_RESOURCES must be a list or tuple") resources = {} for resource in resources_setting: try: path, url, cksm = resource except ValueError: raise ImproperlyConfigured("Each item in settings.REMOTE_FINDER_RESOURCES must be a tuple of three elements (path, url, cksm).") try: hash_type, expected_hexdigest = cksm.split(':') except ValueError: raise ImproperlyConfigured("RemoteFinder checksum `%s` is not in `hash_type:hexdigest` format." % cksm) try: hash_func = hash_func_map[hash_type] except KeyError: raise ImproperlyConfigured("RemoteFinder: hash type `%s` unknown" % hash_type) try: expected_digest = bytearray.fromhex(expected_hexdigest) except ValueError: raise ImproperlyConfigured("Cannot parse hex string in settings.REMOTE_FINDER_RESOURCES: `%s`" % expected_hexdigest) if len(expected_digest) != hash_func().digest_size: raise ImproperlyConfigured("settings.REMOTE_FINDER_RESOURCES: %s digest expected %d bytes but %d provided: `%s`" % (hash_type, hash_func().digest_size, len(expected_digest), expected_hexdigest)) resources[path] = _ResourceInfo(url, hash_func, expected_digest) self.resources = resources def find(self, path, all=False): try: resource_info = self.resources[path] except KeyError: return [] self.fetch(path, resource_info) match = self.storage.path(path) if all: return [match] else: return match def fetch(self, path, resource_info): if self.storage.exists(path): # check to see if the hash has already been verified in the # lifetime of this process if resource_info.hash_verified and not self.always_verify: return # verify the hash f = self.storage.open(path) try: content = f.read() finally: f.close() digest = resource_info.hash_func(content).digest() if digest == resource_info.expected_digest: resource_info.hash_verified = True return # hash verification failed, so delete it from storage and # re-download the file logger.info("Hash verification failed, so deleting %s from storage", path) # The following line does /not/ raise an exception if the file is # already deleted, which is desirable for us as it prevents an # error in the case of a race condition. self.storage.delete(path) # download the file logger.info("Downloading %s", resource_info.url) f = urlopen(resource_info.url) try: content = f.read() finally: f.close() # check its hash digest = resource_info.hash_func(content).digest() if digest != resource_info.expected_digest: raise RuntimeError("Digest for %s does not match expected value given in settings.REMOTE_FINDER_RESOURCES", resource_info.url) # save it name = self.storage.save(path, ContentFile(content)) if name == path: resource_info.hash_verified = True else: logger.warning("Save failed: %r != %r", name, path) def list(self, ignore_patterns): for path, resource_info in self.resources.items(): if matches_patterns(path, ignore_patterns): continue self.fetch(path, resource_info) yield path, self.storage
class SchizophreniaStorage(Storage): SYNCED = 'synced' VERIFIED = 'verified' def __init__(self, source=None, target=None): if not source: source = settings.SCHIZOPHRENIA_SOURCE_STORAGE if not target: target = settings.SCHIZOPHRENIA_TARGET_STORAGE self.source = get_storage(source)() self.target = get_storage(target)() self.downloads = FileSystemStorage(settings.SCHIZOPHRENIA_CACHE_DIR) def download(self, name): """Download file and return instance of local File""" if self.downloads.exists(name): return self.downloads.open(name) remote_file = self.source.open(name) self.downloads.save(name, remote_file) return self.downloads.open(name) def _get_file_cache_key(self, name): return 'schizophrenia_state_%s' % name def sync(self, name, verify=False): """Get file from source storage and upload to target""" logger.debug('Checking cached state ...') # Check cached state, return if synced cache_key = self._get_file_cache_key(name) cached_state = cache.get(cache_key, None) if cached_state == self.VERIFIED: logger.info('File was verified, skipping') cache.set(cache_key, self.VERIFIED) return True elif cached_state == self.SYNCED and not verify: logger.info('File was synced, skipping because verify=False') return True elif cached_state == self.SYNCED or self.target.exists(name): logger.info('File was synced, verifying ...') # If file exists on target, verify. Return if synced try: self.verify(name) except VerificationException: logger.info("File didn't verify, syncing again ...") cached_state = None cache.delete(cache_key) else: logger.info('File verified OK') cache.set(cache_key, self.VERIFIED) return True # Sync logger.debug('Downloading source file ...') local_file = self.download(name) logger.debug('Uploading to target storage ...') self.target.save(name, local_file) cache.set(cache_key, self.SYNCED) # Verify if verify: logger.debug('Verifying ...') try: self.verify(name) logger.debug('Verified OK') cache.set(cache_key, self.VERIFIED) except VerificationException: raise finally: self.downloads.delete(name) self.downloads.delete(name) return True def issynced(self, name): """Does the file exist on target storage?""" return self.target.exists(name) def verify(self, name): if self.target.open(name).read() != self.download(name).read(): raise VerificationException("Sync verification failed for '%s'" % name) return True def cleanup(self, force=False): """Cleanup empty directories that might be left over from downloads""" if force: shutil.rmtree(settings.SCHIZOPHRENIA_CACHE_DIR) else: self._remove_empty_folders(settings.SCHIZOPHRENIA_CACHE_DIR) def _remove_empty_folders(self, path): if not os.path.isdir(path): return # remove empty subfolders files = os.listdir(path) if len(files): for f in files: fullpath = os.path.join(path, f) if os.path.isdir(fullpath): self._remove_empty_folders(fullpath) # if folder empty, delete it files = os.listdir(path) if len(files) == 0: os.rmdir(path) def _open(self, name, *args, **kwargs): """Reads from target storage if verified, otherwise source""" storage = self._get_verified_storage(name) return storage.open(name, *args, **kwargs) def _storage_save(self, storage, name, content): """Save to storage""" try: name = storage.save(name, content) except TypeError: content = CompatibleFile(file=content) name = storage.save(name, content) return name def _save(self, name, content): """Saves both source and target but returns value of target storage""" source_name = self._storage_save(self.source, name, content) target_name = self._storage_save(self.target, name, content) if source_name != target_name: raise ValueError("Storages saved with different names") return target_name def get_available_name(self, name): source_name = self.source.get_available_name(name) target_name = self.target.get_available_name(source_name) if source_name != target_name: raise ValueError("Storages returned different values from " "get_available_name.") return target_name def get_valid_name(self, name): source_name = self.source.get_valid_name(name) target_name = self.target.get_valid_name(name) if source_name != target_name: raise ValueError("Storages returned different values from " "get_valid_name.") return target_name def _get_verified_storage(self, name): if cache.get(self._get_file_cache_key(name), None) == self.VERIFIED: storage = self.target else: storage = self.source return storage def delete(self, name): self.target.delete(name) return self.source.delete(name) def exists(self, name): storage = self._get_verified_storage(name) return storage.exists(name) def listdir(self, path): return self.source.listdir(path) def size(self, name): storage = self._get_verified_storage(name) return storage.size(name) def url(self, name): storage = self._get_verified_storage(name) return storage.url(name)
def exists(self, name): if self.is_thumbor(name): return ThumborStorage.exists(self, name) else: return FileSystemStorage.exists(self, name)
def pdf(self): """ Create the pdf for bill and notice need an authentication in order to be able to render the pdf as login_required decorator is in the pdf_view #http://stackoverflow.com/questions/3222549/how-to-automatically-login-a-user-after-registration-in-django """ print "writing", self.type, self.business_date, self.Model.objects.filter(**{self.filter_attribute:self.business_date}).count() merger = PdfFileMerger() if self.type == 'bill': name = 'Generate Bills PDF' else: name = 'Generate Notices PDF' task, created = models.Task.objects.get_or_create(type=self.type,name=name, status='pending',business_date=self.business_date) objects = self.Model.objects.filter(**{self.filter_attribute:self.business_date}).order_by(*['account__address__address4','account__address__address2','account__address__address3']) task.jobs_total = objects.count() task.jobs_done = 0 task.reading_period = self.billing_schedule.reading_start_date.strftime("%b %d, %Y") + " To " + self.billing_schedule.reading_end_date.strftime("%b %d, %Y") jobs_done = 0 task.save() for i in objects: print self.type, "object", i.pk, i request = self.factory.get(self.url_prefix + str(i.pk) + '/pdf/') #login(request, admin) request.user = self.user response = self.pdf_view(request, **{'pk':i.pk}) response.render() merger.append(StringIO(response.content)) jobs_done+=1 task.jobs_done = jobs_done task.status = 'in progress' task.save() output_file = u'-'.join([ self.file_prefix, u'ALL', self.business_date.strftime(u'%Y-%m-%d')]) + u'.pdf' print "output_file: " + str(output_file) if self.Model.objects.filter(**{self.filter_attribute:self.business_date}).count()>0: fs = FileSystemStorage() # Create a storage instance "fs" from where to store "myfile" f = open(output_file,'wb') # open the output file as "f" merger.write(f) f.close() g = open(output_file,'rb') myfile = File(g) if fs.exists(output_file): print "It exists already! : ", output_file print "Overwriting.." fs.delete(output_file) fs.save(output_file,myfile) os.remove(output_file) from datetime import date filerepo, created = models.FileRepo.objects.get_or_create(file_name=output_file, generation_date=date.today(), business_date=self.business_date) if created: print "created a new file record!" filerepo.file_name = output_file filerepo.file_type = self.type if self.type == 'bill': filerepo.file_description = "all bills" elif self.type == 'notice': filerepo.file_description = "all notices" filerepo.generation_date = date.today() filerepo.business_date = self.business_date filerepo.reading_period = self.reading_period filerepo.save() else: print "File already exists!: ", output_file filerepo.generation_date = date.today() filerepo.save() task.result = "Saved as file: ", output_file else: print "no pdf generateed for", self.file_prefix task.result = "no pdf generateed for", self.file_prefix task.status = 'completed' task.save()
def exist(filename): """ Verifie si le fichier existe """ fileStorage = FileSystemStorage(location="/") return fileStorage.exists(filename)
class SitemapGenerator(object): def __init__(self): self.has_changes = False self.storage = FileSystemStorage(location=ROOT_DIR) self.sitemaps = ROOT_SITEMAP @staticmethod def get_hash(bytestream): return hashlib.md5(bytestream).digest() def read_hash(self, path): with self.storage.open(path) as f: result = self.get_hash(f.read()) return result def write(self): old_index_md5 = None baseurl = _url parts = [] # Collect all pages and write them. for section, site in self.sitemaps.items(): if callable(site): pages = site().paginator.num_pages else: pages = site.paginator.num_pages for page in range(1, pages + 1): filename = FILENAME_TEMPLATE % {'section': section, 'page': page} lastmod = self.write_page(site, page, filename) parts.append({ 'location': '%s%s' % (baseurl, filename), 'lastmod': lastmod }) path = os.path.join(ROOT_DIR, 'sitemap.xml') if self.storage.exists(path): old_index_md5 = self.read_hash(path) self.storage.delete(path) output = loader.render_to_string(INDEX_TEMPLATE, {'sitemaps': parts}) self.storage.save(path, ContentFile(output)) with self.storage.open(path) as sitemap_index: if self.get_hash(sitemap_index.read()) != old_index_md5: self.has_changes = True def write_page(self, site, page, filename): old_page_md5 = None urls = [] try: if callable(site): urls.extend(site().get_urls(page)) else: urls.extend(site.get_urls(page)) except EmptyPage: self.out("Page %s empty" % page) except PageNotAnInteger: self.out("No page '%s'" % page) lastmods = [lastmod for lastmod in [u.get('lastmod') for u in urls] if lastmod is not None] file_lastmod = max(lastmods) if len(lastmods) > 0 else None path = os.path.join(ROOT_DIR, filename) template = getattr(site, 'sitemap_template', 'sitemap.xml') if self.storage.exists(path): old_page_md5 = self.read_hash(path) self.storage.delete(path) output = smart_str(loader.render_to_string(template, {'urlset': urls})) self.storage.save(path, ContentFile(output)) with self.storage.open(path) as sitemap_page: if old_page_md5 != self.get_hash(sitemap_page.read()): self.has_changes = True
def exists(self, name): return FileSystemStorage.exists(self, name)
def exists(self, name): file_system = FileSystemStorage() return file_system.exists(name)
class PeruimUtils(object): def __init__(self): self.fs = FileSystemStorage(location=settings.PERUIM_FILE_PATH) self.samples_dir = "droidbot_samples" self.report_dir = "peruim_user_reports" def get_perm_desc(self, report_path): """ get the permission description of given package :param report_path: report path of target app :return: """ available_reports = self.get_available_reports() if report_path not in available_reports: return None perm_desc_path = "%s/perm_desc.json" % (report_path) storage = self.fs.open(perm_desc_path, 'r') perm_desc = json.load(storage) return perm_desc def get_app_info(self, report_path): app_info = {} app_info['Package'] = report_path app_info['Name'] = 'Unknown' app_info['Category'] = 'Unknown' return app_info def get_state_image(self, report_path, state_tag): image_path = "%s/device_states/snapshot_%s.png" % (report_path, state_tag) # print image_path if self.fs.exists(image_path): image = self.fs.open(image_path, "rb").read() else: image = None return image def get_component_image(self, report_path, component_id): image_path = "%s/components/%s.jpg" % (report_path, component_id) # print image_path if self.fs.exists(image_path): image = self.fs.open(image_path, "rb").read() else: image = None return image def save_report(self, report): print report tag = self.get_time_tag() report_path = "%s/%s_%s.json" % (self.report_dir, tag, report['user_name']) report_file = self.fs.open(report_path, 'w') json.dump(report, report_file) def get_time_tag(self): from datetime import datetime tag = datetime.now().strftime("%Y-%m-%d_%H%M%S") return tag def get_available_reports(self): available_reports = [] base_path = self.fs.path('.') if self.fs.exists(self.samples_dir): import os for root, dirs, files in os.walk(base_path): for f in files: if f == "perm_desc.json": report_path = os.path.relpath(root, base_path) # print report_path available_reports.append(report_path) # available_packages = self.fs.listdir(self.samples_dir) # print available_packages # for package in available_packages[0]: # perm_desc_path = "%s/%s/perm_desc.json" % (self.samples_dir, package) # if self.fs.exists(perm_desc_path): # available_reports.append(package) return available_reports
def save(request, form, image, saveType, url = None): #Loads an instace of FileSystemStorage store = FileSystemStorage(paths.SITE_ROOT + '/images/') if image: #Checks if an image already exists #If it does, use that image as the url #Else save the image, and create a url if store.exists(image.name): imageURL = '/images/' + image.name featuredImage = imageURL logging.basicConfig(filename=paths.SITE_ROOT + '/output.log', level=logging.DEBUG) logging.debug("EXISTS") else: storedImage = store.save(image.name, image) imageURL = "/images/" + storedImage featuredImage = imageURL else: imageURL = None featuredImage = None if form.is_valid(): if saveType == "Post": if url: #If the post is being edited, delete the older version published = Post.objects.get(pk=url).published Post.objects.get(pk=url).delete() posts = Categories.objects.filter(Post__id = url) for i in posts: i.delete() featured = Featured.objects.filter(Post__id = url) if featured: featured.delete() else: published = datetime.datetime.now() month = published.month if month < 10: newmonth = '0' + str(month) else: newmonth = str(month) #Create a post with the correct information title = request.POST['title'] linkTitle = title.replace(' ', '-').lower() exclude = ['$', '#', '&', ';', ',', '"', '?'] linkTitle = ''.join(ch for ch in linkTitle if ch not in exclude) link = str(published.year) + '/' + newmonth + '/' + linkTitle + '/' if not 'imageInPost' in request.POST: imageURL = None else: if not imageURL: return "You didn't upload an image.", None post = Post(title = request.POST['title'], sourceUrl = request.POST['sourceUrl'], image = imageURL, content = request.POST['content'], published = published, link = link) post.save() if 'featured' in request.POST: if not featuredImage: return "You need an image", post featured = Featured(Post = post, box = 1, imageURL = featuredImage) featured.save() if 'categories' in request.POST: categories = request.POST.getlist('categories') for category in categories: newCategory = Categories(Post = post, category = category) newCategory.save() return None, post else: return "Something is amiss", post elif saveType == "Page": if url: #If the post is being edited, delete the older version Page.objects.get(title=url).delete() #Create a post with the correct information page = Page(title = request.POST['title'], image = imageURL, content = request.POST['content']) page.save() return None else: return "Something is amiss...", None