def _new_file(self, key_name, etag, dirinfo): ''' etag是md5值 ''' # 如果是文件夹,返回 if key_name.endswith(r'/'): return unicode_key_name = key_name.decode('utf8') savename = path_join(self._path, self._bucket.name.decode('utf8'), unicode_key_name) etag = etag.replace('"', '').lower() if os.path.exists(savename): fileinfo = dirinfo.get(savename) if fileinfo and not fileinfo.is_changed() and fileinfo.md5 == etag: logging.info('file not changed') return elif fileinfo and fileinfo.is_changed(): if fileinfo.md5 != etag and fileutils.md5(savename) != etag: logging.error('file [%s] conflict.' % unicode_key_name) print "TODO:旧文件存在,并发了改变,生成冲突,请用户选择:覆盖还是跳过 1" elif fileinfo.md5 == etag: logging.info('the same file md5, just mtime changed.') dirinfo[savename] = FileInfo(savename, etag) dirinfo.save() return elif not fileinfo: if fileutils.md5(savename) == etag: logging.info('the same file md5.') dirinfo[savename] = FileInfo(savename, etag) dirinfo.save() return else: logging.warning('file [%s] conflict.' % unicode_key_name) print "TODO:文件存在,第一次同步的,生成冲突,请用户选择:覆盖还是跳过 2" return return self.__download(unicode_key_name, etag, savename, dirinfo)
def upload_file(self, path, key, dirinfo): fileinfo = dirinfo.get(path) if fileinfo is None: return self._upload_file(path, key, dirinfo) # 检验文件是否与服务端的一致 if fileinfo.is_changed(): md5 = fileutils.md5(path) if md5 and fileinfo.md5 != md5: logging.info('file[%s] changed,upload for update.' % key) return self._upload_file(path, key) else: # md5 一样,mtime可能变了 dirinfo[path] = FileInfo(path, md5) dirinfo.save() logging.info('file not changed [%s]' % key)