def clean_dir_name(self): if self.cleaned_data['dir_name']: # only letters, numbers, underscores, spaces and hyphens are allowed. if not alnum_name_re.search(self.cleaned_data['dir_name']): raise forms.ValidationError(_(u'Only letters, numbers, underscores, spaces and hyphens are allowed.')) # Folder must not already exist. if os.path.isdir(os.path.join(self.path, convert_filename(self.cleaned_data['dir_name']))): raise forms.ValidationError(_(u'The Folder already exists.')) return convert_filename(self.cleaned_data['dir_name'])
def _upload_file(request): """ Upload file to the server. Implement unicode handlers - https://github.com/sehmaschine/django-filebrowser/blob/master/filebrowser/sites.py#L471 """ if request.method == 'POST': folder = request.POST.get('folder') fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("fb_upload")) folder = fb_uploadurl_re.sub('', folder) if request.FILES: filedata = request.FILES['Filedata'] # PRE UPLOAD SIGNAL filebrowser_pre_upload.send(sender=request, path=request.POST.get('folder'), file=filedata) filedata.name = convert_filename(filedata.name) # HANDLE UPLOAD exists = default_storage.exists(os.path.join(get_directory(), folder, filedata.name)) abs_path = os.path.join(get_directory(), folder, filedata.name) uploadedfile = default_storage.save(abs_path, filedata) path = os.path.join(get_directory(), folder) file_name = os.path.join(path, filedata.name) if exists: default_storage.move(smart_text(uploadedfile), smart_text(file_name), allow_overwrite=True) # POST UPLOAD SIGNAL filebrowser_post_upload.send(sender=request, path=request.POST.get('folder'), file=FileObject(smart_text(file_name))) return HttpResponse('True')
def _upload_file(request): """ Upload file to the server. Implement unicode handlers - https://github.com/sehmaschine/django-filebrowser/blob/master/filebrowser/sites.py#L471 """ if request.method == 'POST': folder = request.POST.get('folder') fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("fb_upload")) folder = fb_uploadurl_re.sub('', folder) if request.FILES: filedata = request.FILES['Filedata'] directory = get_directory() # PRE UPLOAD SIGNAL filebrowser_pre_upload.send(sender=request, path=request.POST.get('folder'), file=filedata) # Try and remove both original and normalised thumb names, # in case files were added programmatically outside FB. file_path = os.path.join(directory, folder, filedata.name) remove_thumbnails(file_path) filedata.name = convert_filename(filedata.name) file_path = os.path.join(directory, folder, filedata.name) remove_thumbnails(file_path) # HANDLE UPLOAD uploadedfile = default_storage.save(file_path, filedata) if default_storage.exists(file_path): default_storage.move(smart_text(uploadedfile), smart_text(file_path), allow_overwrite=True) # POST UPLOAD SIGNAL filebrowser_post_upload.send(sender=request, path=request.POST.get('folder'), file=FileObject(smart_text(file_path))) return HttpResponse('True')
def _upload_file(request): """ Upload file to the server. """ from django.core.files.move import file_move_safe if request.method == 'POST': folder = request.POST.get('folder') fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("fb_upload")) folder = fb_uploadurl_re.sub('', folder) abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, folder) if request.FILES: filedata = request.FILES['Filedata'] filedata.name = convert_filename(filedata.name) # PRE UPLOAD SIGNAL filebrowser_pre_upload.send(sender=request, path=request.POST.get('folder'), file=filedata) # HANDLE UPLOAD uploadedfile = handle_file_upload(abs_path, filedata) # MOVE UPLOADED FILE # if file already exists if os.path.isfile(os.path.join(MEDIA_ROOT, DIRECTORY, folder, filedata.name)): old_file = os.path.join(abs_path, filedata.name) new_file = os.path.join(abs_path, uploadedfile) file_move_safe(new_file, old_file) # POST UPLOAD SIGNAL filebrowser_post_upload.send(sender=request, path=request.POST.get('folder'), file=FileObject(os.path.join(DIRECTORY, folder, filedata.name))) return HttpResponse('True')
def _upload_file(request): """ Upload file to the server. """ if request.method == "POST": folder = request.POST.get("folder") fb_uploadurl_re = re.compile(r"^.*(%s)" % reverse("fb_upload")) folder = fb_uploadurl_re.sub("", folder) if "." in folder: return HttpResponseBadRequest("") if request.FILES: filedata = request.FILES["Filedata"] directory = get_directory() # Validate file against EXTENSIONS setting. if not get_file_type(filedata.name): return HttpResponseBadRequest("") # PRE UPLOAD SIGNAL filebrowser_pre_upload.send( sender=request, path=request.POST.get("folder"), file=filedata ) # Try and remove both original and normalised thumb names, # in case files were added programmatically outside FB. file_path = os.path.join(directory, folder, filedata.name) remove_thumbnails(file_path) filedata.name = convert_filename(filedata.name) file_path = os.path.join(directory, folder, filedata.name) remove_thumbnails(file_path) if ( "." in file_path and file_path.split(".")[-1].lower() in fb_settings.ESCAPED_EXTENSIONS ): filedata = ContentFile(escape(filedata.read()), name=filedata.name) # HANDLE UPLOAD uploadedfile = default_storage.save(file_path, filedata) if default_storage.exists(file_path) and file_path != uploadedfile: default_storage.move( smart_text(uploadedfile), smart_text(file_path), allow_overwrite=True, ) # POST UPLOAD SIGNAL filebrowser_post_upload.send( sender=request, path=request.POST.get("folder"), file=FileObject(smart_text(file_path)), ) get_params = request.POST.get("get_params") if get_params: return HttpResponseRedirect(reverse("fb_browse") + get_params) return HttpResponse("True")
def save(self, delete_zip_import=True, *args, **kwargs): """ If a zip file is uploaded, extract any images from it and add them to the gallery, before removing the zip file. """ self.gen_description = False if not hasattr(self, 'image_collection'): new_image_collection = ImageCollection(title=self.title) new_image_collection.save() self.image_collection = new_image_collection # audio = [album_image.audio_file for album_image in self.images.all() if album_image.audio_file] # if(any(audio)): # soundcloud_helper = SoundCloud() # soundcloud_helper.create_playlist(self.title) super(Album, self).save(*args, **kwargs) if self.zip_import: zip_file = ZipFile(self.zip_import) # import PIL in either of the two ways it can end up installed. try: from PIL import Image except ImportError: import Image first = True for name in zip_file.namelist(): data = zip_file.read(name) try: image = Image.open(StringIO(data)) image.load() image = Image.open(StringIO(data)) image.verify() except: continue name = convert_filename(os.path.split(name)[1]) path = os.path.join(ALBUMS_UPLOAD_DIR, self.slug, name.decode("utf-8")) try: saved_path = default_storage.save(path, ContentFile(data)) except UnicodeEncodeError: from warnings import warn warn("A file was saved that contains unicode " "characters in its path, but somehow the current " "locale does not support utf-8. You may need to set " "'LC_ALL' to a correct value, eg: 'en_US.UTF-8'.") path = os.path.join(ALBUMS_UPLOAD_DIR, self.slug, unicode(name, errors="ignore")) saved_path = default_storage.save(path, ContentFile(data)) album_image = AlbumImage(image_file=saved_path, location=self.location, photographer=self.photographer) if first and not self.has_cover: album_image.is_cover = True first = False self.images.add(album_image) if delete_zip_import: zip_file.close() self.zip_import.delete(save=True)
def save(self, delete_zip_import=True, *args, **kwargs): """ If a zip file is uploaded, extract any images from it and add them to the gallery, before removing the zip file. """ # Update if a entry for district already exists face_exist = Face.objects.filter(district=self.district) face_exist = face_exist and face_exist[0] if not self.pk and face_exist: self.image_collection = face_exist.image_collection self.image_collection_id = face_exist.image_collection_id self.pk = face_exist.pk self.site_id = face_exist.site_id if not hasattr(self, 'image_collection'): new_image_collection = ImageCollection(title=self.district.district) new_image_collection.save() self.image_collection = new_image_collection super(Face, self).save(*args, **kwargs) if self.zip_import: zip_file = ZipFile(self.zip_import) # import PIL in either of the two ways it can end up installed. try: from PIL import Image except ImportError: import Image for name in zip_file.namelist(): data = zip_file.read(name) try: image = Image.open(StringIO(data)) image.load() image = Image.open(StringIO(data)) image.verify() except: continue name = convert_filename(os.path.split(name)[1]) path = os.path.join(FACES_UPLOAD_DIR, self.slug, name.decode("utf-8")) try: saved_path = default_storage.save(path, ContentFile(data)) except UnicodeEncodeError: from warnings import warn warn("A file was saved that contains unicode " "characters in its path, but somehow the current " "locale does not support utf-8. You may need to set " "'LC_ALL' to a correct value, eg: 'en_US.UTF-8'.") path = os.path.join(FACES_UPLOAD_DIR, self.slug, unicode(name, errors="ignore")) saved_path = default_storage.save(path, ContentFile(data)) face_image = FaceImage(image_file=saved_path) self.images.add(face_image) if delete_zip_import: zip_file.close() self.zip_import.delete(save=True)
def _upload_file(request): """ Upload file to the server. """ if request.method == 'POST': folder = request.POST.get('folder') fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("fb_upload")) folder = fb_uploadurl_re.sub('', folder) if ".." in folder: return HttpResponseBadRequest("") if request.FILES: filedata = request.FILES['Filedata'] directory = get_directory() # Validate file against EXTENSIONS setting. if not get_file_type(filedata.name): return HttpResponseBadRequest("") # PRE UPLOAD SIGNAL filebrowser_pre_upload.send(sender=request, path=request.POST.get('folder'), file=filedata) # Try and remove both original and normalised thumb names, # in case files were added programmatically outside FB. file_path = os.path.join(directory, folder, filedata.name) remove_thumbnails(file_path) filedata.name = convert_filename(filedata.name) # this won't work with windows and s3 - replace the "\" file_path = os.path.join(directory, folder, filedata.name).replace("\\", "/") remove_thumbnails(file_path) if "." in file_path and file_path.split( ".")[-1].lower() in ESCAPED_EXTENSIONS: filedata = ContentFile(escape(filedata.read()), name=filedata.name) # HANDLE UPLOAD uploadedfile = default_storage.save(file_path, filedata) if default_storage.exists(file_path) and file_path != uploadedfile: default_storage.move(smart_text(uploadedfile), smart_text(file_path), allow_overwrite=True) # POST UPLOAD SIGNAL filebrowser_post_upload.send(sender=request, path=request.POST.get('folder'), file=FileObject( smart_text(file_path))) get_params = request.POST.get('get_params') if get_params: return HttpResponseRedirect(reverse('fb_browse') + get_params) return HttpResponse('True')
def _upload_file(request): """ Upload file to the server. Implement unicode handlers - https://github.com/sehmaschine/django-filebrowser/blob/master/filebrowser/sites.py#L471 """ if request.method == 'POST': folder = request.POST.get('folder') fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("fb_upload")) folder = fb_uploadurl_re.sub('', folder) if "." in folder: return HttpResponseBadRequest("") if request.FILES: filedata = request.FILES['Filedata'] directory = get_directory() # Validate file against EXTENSIONS setting. if not get_file_type(filedata.name): return HttpResponseBadRequest("") # PRE UPLOAD SIGNAL filebrowser_pre_upload.send(sender=request, path=request.POST.get('folder'), file=filedata) # Try and remove both original and normalised thumb names, # in case files were added programmatically outside FB. file_path = os.path.join(directory, folder, filedata.name) remove_thumbnails(file_path) filedata.name = convert_filename(filedata.name) file_path = os.path.join(directory, folder, filedata.name) remove_thumbnails(file_path) # HANDLE UPLOAD uploadedfile = default_storage.save(file_path, filedata) if default_storage.exists(file_path) and file_path != uploadedfile: default_storage.move(smart_text(uploadedfile), smart_text(file_path), allow_overwrite=True) # POST UPLOAD SIGNAL filebrowser_post_upload.send(sender=request, path=request.POST.get('folder'), file=FileObject( smart_text(file_path))) get_params = request.POST.get('get_params') if get_params: return HttpResponseRedirect(reverse('fb_browse') + get_params) return HttpResponse('True')
def _upload_file(request): """ Upload file to the server. """ if request.method == "POST": folder = request.POST.get("folder") fb_uploadurl_re = re.compile(r"^.*(%s)" % reverse("fb_upload")) folder = fb_uploadurl_re.sub("", folder) if "." in folder: return HttpResponseBadRequest("") if request.FILES: filedata = request.FILES["Filedata"] directory = get_directory() # Validate file against EXTENSIONS setting. if not get_file_type(filedata.name): return HttpResponseBadRequest("") # PRE UPLOAD SIGNAL filebrowser_pre_upload.send(sender=request, path=request.POST.get("folder"), file=filedata) # Try and remove both original and normalised thumb names, # in case files were added programmatically outside FB. file_path = os.path.join(directory, folder, filedata.name) remove_thumbnails(file_path) filedata.name = convert_filename(filedata.name) file_path = os.path.join(directory, folder, filedata.name) remove_thumbnails(file_path) if "." in file_path and file_path.split(".")[-1].lower() in ESCAPED_EXTENSIONS: filedata = ContentFile(escape(filedata.read()), name=filedata.name) # HANDLE UPLOAD uploadedfile = default_storage.save(file_path, filedata) if default_storage.exists(file_path) and file_path != uploadedfile: default_storage.move(smart_text(uploadedfile), smart_text(file_path), allow_overwrite=True) # POST UPLOAD SIGNAL filebrowser_post_upload.send( sender=request, path=request.POST.get("folder"), file=FileObject(smart_text(file_path)) ) get_params = request.POST.get("get_params") if get_params: return HttpResponseRedirect(reverse("fb_browse") + get_params) return HttpResponse("True")
def _check_file(request): """ Check if file already exists on the server. """ from django.utils import simplejson folder = request.POST.get('folder') fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("fb_upload")) folder = fb_uploadurl_re.sub('', folder) fileArray = {} if request.method == 'POST': for k,v in request.POST.items(): if k != "folder": v = convert_filename(v) if os.path.isfile(os.path.join(MEDIA_ROOT, DIRECTORY, folder, v)): fileArray[k] = v return HttpResponse(simplejson.dumps(fileArray))
def _check_file(request): """ Check if file already exists on the server. """ from django.utils import simplejson folder = request.POST.get('folder') fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("fb_upload")) folder = fb_uploadurl_re.sub('', folder) fileArray = {} if request.method == 'POST': for k, v in request.POST.items(): if k != "folder": v = convert_filename(v) if os.path.isfile( os.path.join(MEDIA_ROOT, DIRECTORY, folder, v)): fileArray[k] = v return HttpResponse(simplejson.dumps(fileArray))
def _upload_file(request): """ Upload file to the server. Implement unicode handlers - https://github.com/sehmaschine/django-filebrowser/blob/master/filebrowser/sites.py#L471 """ if request.method == 'POST': folder = request.POST.get('folder') fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("fb_upload")) folder = fb_uploadurl_re.sub('', folder) if request.FILES: filedata = request.FILES['Filedata'] # PRE UPLOAD SIGNAL filebrowser_pre_upload.send(sender=request, path=request.POST.get('folder'), file=filedata) filedata.name = convert_filename(filedata.name) # HANDLE UPLOAD exists = default_storage.exists( os.path.join(get_directory(), folder, filedata.name)) abs_path = os.path.join(get_directory(), folder, filedata.name) uploadedfile = default_storage.save(abs_path, filedata) path = os.path.join(get_directory(), folder) file_name = os.path.join(path, filedata.name) if exists: default_storage.move(smart_text(uploadedfile), smart_text(file_name), allow_overwrite=True) # POST UPLOAD SIGNAL filebrowser_post_upload.send(sender=request, path=request.POST.get('folder'), file=FileObject( smart_text(file_name))) return HttpResponse('True')
def _upload_file(request): """ Upload file to the server. """ from django.core.files.move import file_move_safe if request.method == 'POST': folder = request.POST.get('folder') fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("fb_upload")) folder = fb_uploadurl_re.sub('', folder) abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, folder) if request.FILES: filedata = request.FILES['Filedata'] filedata.name = convert_filename(filedata.name) # PRE UPLOAD SIGNAL filebrowser_pre_upload.send(sender=request, path=request.POST.get('folder'), file=filedata) # HANDLE UPLOAD uploadedfile = handle_file_upload(abs_path, filedata) # MOVE UPLOADED FILE # if file already exists if os.path.isfile( os.path.join(MEDIA_ROOT, DIRECTORY, folder, filedata.name)): old_file = os.path.join(abs_path, filedata.name) new_file = os.path.join(abs_path, uploadedfile) file_move_safe(new_file, old_file) # POST UPLOAD SIGNAL filebrowser_post_upload.send( sender=request, path=request.POST.get('folder'), file=FileObject(os.path.join(DIRECTORY, folder, filedata.name))) return HttpResponse('True')