开发者

Lock directory when uploading to it

I've been thinking of updating the directory name with username and datetime when an user uploads files to it. So that the newest upload from a user would show on it. I have a upload function and rename like so:

# Check if form is valid and upload   
    if form.is_valid():
        form.save(request.FILES, request)
    # Edit the previous folder to have new datetime and user marking if the folder has such.
    currentPath = post_data.get('path').encode("utf-8")
    prevfolder = os.path.basename(post_data.get('path').encode("utf-8"))
    try:
        casename, rest = prevfolder.split(" [",1)
    #print(casename)
        dest = renameOnUpload(request,currentPath, casename)
    except:
        dest = form.path

        return HttpResponseRedirect('/fm/list/%s' % dest)

def renameOnUpload(request,path,casename):
    datetime_string = get_currenttime()
    user_string = " ["+ request.user.username + "]"
    newcasename = casename+user_string.encode("utf-8")+datetime_string
    dest = os.path.join(os.path.dirname(path), newcasename)
    if not path == dest:
        fmoper.move(path, dest)
    else:
        dest = path
    return dest

fmoper.move

def move(src, dst, replace=False)开发者_开发问答:
    ensure_dir(dst)             # Ensure that we have a destination folder, if not create it. 
    if not replace and os.path.exists(dst):
        dst = existname(dst)
    return shutil.move(src, dst)

So it uploads the files and does a move after. Which causes problems when an user is uploading and another one uploads at the same time, renaming the folder in the process causing one uploader to upload to an folder that doesn't exist.

Is there some neat way I could lock the folder when an user is uploading to it? currentPath is the folder uploaded to and dest the renamed one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜