def modify_name(request, share, subdir=None): import os form = RenameForm(request.POST) data = json_form_validate(form) if form.is_valid(): if subdir is None: from_path = os.path.join(share.get_path(), form.cleaned_data['from_name']) to_path = os.path.join(share.get_path(), form.cleaned_data['to_name']) else: from_path = os.path.join(share.get_path(), subdir, form.cleaned_data['from_name']) to_path = os.path.join(share.get_path(), subdir, form.cleaned_data['to_name']) os.rename(from_path, to_path) data['objects'] = [{ 'from_name': form.cleaned_data['from_name'], 'to_name': form.cleaned_data['to_name'] }] ShareLog.create( share=share, user=request.user, action=ShareLog.ACTION_RENAMED, text='"%s" renamed to "%s"' % (form.cleaned_data['from_name'], form.cleaned_data['to_name']), paths=[from_path], subdir=subdir) return json_response(data)
def create_folder(request, share, subdir=None): form = FolderForm(request.POST) data = json_form_validate(form) if form.is_valid(): folder_path = share.create_folder(form.cleaned_data['name'],subdir) (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(folder_path) data['objects']=[{'name':form.cleaned_data['name'],'modified':datetime.datetime.fromtimestamp(mtime).strftime("%m/%d/%Y %I:%M %p")}] ShareLog.create(share=share,user=request.user,action=ShareLog.ACTION_FOLDER_CREATED,paths=[form.cleaned_data['name']],subdir=subdir) return json_response(data) else: return json_error([error for name, error in form.errors.items()])
def upload_file(request, share, subdir=None): from os.path import join os.umask(settings.UMASK) PATH = share.get_path() if subdir is not None: PATH = join(PATH, subdir) data = { 'share': share.id, 'subdir': subdir, 'files': [] } #{key:val for key,val in request.POST.iteritems()} for name, file in request.FILES.iteritems(): filename = clean_filename(file.name) FILE_PATH = join(PATH, filename) handle_uploaded_file(FILE_PATH, file) subpath = filename if subdir is None else subdir + filename url = reverse('download_file', kwargs={ 'share': share.id, 'subpath': subpath }) (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(FILE_PATH) data['files'].append({ 'name': filename, 'extension': filename.split('.').pop() if '.' in filename else '', 'size': sizeof_fmt(size), 'bytes': size, 'url': url, 'modified': datetime.datetime.fromtimestamp(mtime).strftime("%m/%d/%Y %H:%M"), 'isText': istext(FILE_PATH) }) # response['url']=reverse('download_file',kwargs={'share':share.id,'subpath':details['subpath']}) # url 'download_file' share=share.id subpath=subdir|default_if_none:""|add:file.name ShareLog.create( share=share, user=request.user, action=ShareLog.ACTION_FILE_ADDED, paths=[clean_filename(file.name) for file in request.FILES.values()], subdir=subdir) return json_response(data)
def delete_paths(request, share, subdir=None, json={}): response={'deleted':[],'failed':[]} for item in json['selection']: test_path(item) item_path = item if subdir is None else os.path.join(subdir,item) try: if share.delete_path(item_path): response['deleted'].append(item) else: response['failed'].append(item) except: response['failed'].append(item) ShareLog.create(share=share,user=request.user,action=ShareLog.ACTION_DELETED,paths=json['selection'],subdir=subdir) return json_response(response)
def modify_name(request, share, subdir=None): import os form = RenameForm(request.POST) data = json_form_validate(form) if form.is_valid(): if subdir is None: from_path = os.path.join(share.get_path(),form.cleaned_data['from_name']) to_path = os.path.join(share.get_path(),form.cleaned_data['to_name']) else: from_path = os.path.join(share.get_path(),subdir,form.cleaned_data['from_name']) to_path = os.path.join(share.get_path(),subdir,form.cleaned_data['to_name']) os.rename(from_path, to_path) data['objects']=[{'from_name':form.cleaned_data['from_name'],'to_name':form.cleaned_data['to_name']}] ShareLog.create(share=share,user=request.user,action=ShareLog.ACTION_RENAMED,text='"%s" renamed to "%s"'%(form.cleaned_data['from_name'],form.cleaned_data['to_name']),paths=[form.cleaned_data['to_name']],subdir=subdir) return json_response(data)
def upload_file(request, share, subdir=None): from os.path import join os.umask(settings.UMASK) PATH = share.get_path() if subdir is not None: PATH = join(PATH,subdir) data = {'share':share.id,'subdir':subdir,'files':[]}#{key:val for key,val in request.POST.iteritems()} for name,file in request.FILES.iteritems(): FILE_PATH = join(PATH,file.name) handle_uploaded_file(FILE_PATH,file) subpath = file.name if subdir is None else subdir + file.name url = reverse('download_file',kwargs={'share':share.id,'subpath':subpath}) (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(FILE_PATH) data['files'].append({'name':file.name,'extension':file.name.split('.').pop() if '.' in file.name else '','size':sizeof_fmt(size),'bytes':size, 'url':url,'modified':datetime.datetime.fromtimestamp(mtime).strftime("%m/%d/%Y %I:%M %p"), 'isText':istext(FILE_PATH)}) # response['url']=reverse('download_file',kwargs={'share':share.id,'subpath':details['subpath']}) # url 'download_file' share=share.id subpath=subdir|default_if_none:""|add:file.name ShareLog.create(share=share,user=request.user,action=ShareLog.ACTION_FILE_ADDED,paths=[file.name for file in request.FILES.values()],subdir=subdir) return json_response(data)
response = {'moved': [], 'failed': []} for item in json['selection']: test_path(item) item_subpath = item if subdir is None else os.path.join(subdir, item) try: if share.move_path(item_subpath, json['destination']): response['moved'].append(item) else: response['failed'].append(item) except Exception, e: pass text = '%s moved from "%s" to "%s"' % (', '.join( response['moved']), subdir if subdir else '', json['destination']) ShareLog.create(share=share, user=request.user, action=ShareLog.ACTION_MOVED, text=text, paths=json['selection'], subdir=subdir) return json_response(response) @safe_path_decorator(path_param='subdir') @share_access_decorator(['download_share_files']) # @JSONDecorator def download_archive_stream(request, share, subdir=None): # try: selection = request.GET.get('selection', '').split(',') for item in selection: test_path(item) try: return share.create_archive_stream(items=selection, subdir=subdir)
@share_access_decorator(['delete_share_files']) @JSONDecorator def move_paths(request, share, subdir=None, json={}): response={'moved':[],'failed':[]} for item in json['selection']: test_path(item) item_subpath = item if subdir is None else os.path.join(subdir,item) try: if share.move_path(item_subpath,json['destination']): response['moved'].append(item) else: response['failed'].append(item) except Exception, e: pass text = '%s moved from "%s" to "%s"' % (', '.join(response['moved']), subdir if subdir else '', json['destination']) ShareLog.create(share=share,user=request.user,action=ShareLog.ACTION_MOVED,text=text,paths=json['selection'],subdir=subdir) return json_response(response) @safe_path_decorator(path_param='subdir') @share_access_decorator(['download_share_files']) # @JSONDecorator def download_archive_stream(request, share, subdir=None): # try: selection = request.GET.get('selection').split(',') for item in selection: test_path(item) try: return share.create_archive_stream(items=selection,subdir=subdir) except Exception, e: return json_error([e.message])