def appUpdateCatalog(): result = checkApiParam(True, [ {"name": "id", "checkfunc": unit.checkParamForInt}, {"name": "parentId", "checkfunc": unit.checkParamForInt, "default": None}, {"name": "name", "checkfunc": lambda v: v if len(v) >= 1 and len(v) < 100 else None, "default": None}, {"name": "memo", "default": None}, {"name": "helpInt", "default": None}, {"name": "helpText", "default": None}]) if not result[kParamForResult]: return result[kParamForErrorResponse] loginInfo = result[kParamForLoginInfo] param = result[kParamForRequestParams] nId = param.pop("id") unit.filterNullValue(param) bOK = False if len(param) > 0: db = _getDbManager() bOK = db.updateCatalog(nId, param, loginInfo.rootIdsString) if bOK: item = db.getCatalogById(nId) return responseHelp.buildSuccessResponseData(dataManager.buildCatalogInfo(item, db)) return responseHelp.buildErrorResponseData(responseHelp.kCmdUserError_NotModify)
def appCreateCatalog(): "创建目录" curTimestamp = unit.getTimeInt() result = checkApiParam(True, [ {"name": "parentId", "checkfunc": unit.checkParamForInt}, {"name": "name", "checkfunc": lambda v: v if len(v) >= 1 and len(v) < 100 else None}, {"name": "createTime", "checkfunc": unit.checkParamForTimestamp, "default": curTimestamp}, {"name": "lastModifyTime", "checkfunc": unit.checkParamForTimestamp, "default": curTimestamp}, {"name": "memo", "default": None}, {"name": "helpInt", "default": None}, {"name": "helpText", "default": None}]) if not result[kParamForResult]: return result[kParamForErrorResponse] loginInfo = result[kParamForLoginInfo] param = result[kParamForRequestParams] #查询数据 db = _getDbManager() parentItem = db.getCatalogByIdAndRootIds(param["parentId"], loginInfo.rootIdsString) if not parentItem: return responseHelp.buildErrorResponseData(responseHelp.kCmdUserError_CatalogIdInValid) #创建路径 strPath = unit.makeUserCreateCatalogPath(parentItem[dataManager.kCatalogFieldPath], param["name"]) if not strPath: return responseHelp.buildErrorResponseData(responseHelp.kCmdServerError_DbDataError) param["path"] = strPath item = db.makeCatalog(param) return responseHelp.buildSuccessResponseData(dataManager.buildCatalogInfo(item, db))
def appGetCatalogs(): "获取指定目录下的子目录信息" # 参数判断 result = checkApiParam(True, [{"name": "pids", "checkfunc": unit.checkParamForIntList}]) if not result[kParamForResult]: return result[kParamForErrorResponse] loginInfo = result[kParamForLoginInfo] strParentIds = result[kParamForRequestParams]["pids"] # 查询数据 db = _getDbManager() dbItems = db.getCatalogsByParentIds(strParentIds, loginInfo.rootIdsString) if strParentIds == "-1" and (not dbItems or len(dbItems) == 0): bOK, resp = _makeUserRootPath(loginInfo.userId) if not bOK: return resp else: dbItems = db.getCatalogsByParentIds(strParentIds, loginInfo.rootIdsString) #生成数据 ltData = []; for item in dbItems: ltData.append(dataManager.buildCatalogInfo(item, db)) return responseHelp.buildSuccessResponseData(ltData)
def appSetHelpInfo(): "设置指定记录的辅助信息" # 参数判断 result = checkApiParam(True, [ {"name": "type", "checkfunc": unit.checkParamForInt}, {"name": "id", "checkfunc": unit.checkParamForInt, "default": -1}, {"name": "helpInt", "checkfunc": unit.checkParamForInt, "default": None}, {"name": "helpText", "default": None}]) if not result[kParamForResult]: return result[kParamForErrorResponse] userLoginInfo = result[kParamForLoginInfo] param = result[kParamForRequestParams] nType = param["type"] nId = userLoginInfo.userId if nType == 0 else param["id"] if nType not in (0, 1, 2) or nId <= 0: return responseHelp.buildErrorResponseData(responseHelp.kCmdUserError_Param) nHelpId = param["helpInt"] strHelpText = param["helpText"] # 设置 if nHelpId != None or strHelpText != None: db = _getDbManager() db.setHelpInfo(nType, nId, nHelpId, strHelpText, userLoginInfo.rootIdsString) return responseHelp.buildSuccessResponseData(None) return responseHelp.buildErrorResponseData(kCmdUserError_Param)
def appGetHelpInfo(): "获取指定记录的辅助信息" # 参数判断 result = checkApiParam(True, [ {"name": "type", "checkfunc": unit.checkParamForInt}, {"name": "id", "checkfunc": unit.checkParamForInt, "default": -1}]) if not result[kParamForResult]: return result[kParamForErrorResponse] userLoginInfo = result[kParamForLoginInfo] param = result[kParamForRequestParams] nType = param["type"] nId = userLoginInfo.userId if nType == 0 else param["id"] if nType not in (0, 1, 2) or nId <= 0: return responseHelp.buildErrorResponseData(responseHelp.kCmdUserError_Param) # 查询并返回数据 db = _getDbManager() hi = db.getHelpInfo(nType, nId, userLoginInfo.rootIdsString) nLen = len(hi) if hi else 0 ltData = {"helpInt": hi[0] if nLen > 0 else None, "helpText": hi[1] if nLen > 1 else None, "lastModifyTime": hi[2] if nLen > 2 else None} unit.filterNullValue(ltData) return responseHelp.buildSuccessResponseData(ltData)
def appLogout(): "退出" key = session.get(defines.kSessionUserKey) user = LoginInfo.GetObject(key) if user: LoginInfo.DeleteObject(key) session.clear(); print("logou id:", user.userId) return responseHelp.buildSuccessResponseData("");
def appShareFileUrl(): "获取指定资源的分享KEY" result = _getFileInfo(None, None) if not result[kParamForResult]: return result[kParamForErrorResponse] param = result[kParamForRequestParams] strFile = param["_x_file"] key = hashlib.md5(strFile.encode("utf8")).hexdigest() cache.getAppFileCache().set(key, strFile) return responseHelp.buildSuccessResponseData(key)
def appUploadingInfo(): "获取所有正在上传的文件" result = checkApiParam(True, ()) if not result[kParamForResult]: return result[kParamForErrorResponse] loginInfo = result[kParamForLoginInfo] # 查询数据 db = _getDbManager(); fileRows = db.getFileByUploading(loginInfo.userId) #生成数据 ltData = dataManager.buildFileInfoList(fileRows, db) return responseHelp.buildSuccessResponseData(ltData)
def appDeleteFiles(): "获取指定目录下的文件" result = checkApiParam(True, ({"name": "ids", "checkfunc": unit.checkParamForIntList},)) if not result[kParamForResult]: return result[kParamForErrorResponse] loginInfo = result[kParamForLoginInfo] param = result[kParamForRequestParams] # 查询数据 db = _getDbManager() db.deleteFiles(param["ids"], loginInfo.rootIdsString) return responseHelp.buildSuccessResponseData("OK")
def appDeleteCatalog(): "删除目录" result = checkApiParam(True, [{"name": "ids", "checkfunc": unit.checkParamForIntList}]) if not result[kParamForResult]: return result[kParamForErrorResponse] loginInfo = result[kParamForLoginInfo] param = result[kParamForRequestParams] db = _getDbManager() try: db.deleteCatalogs(param["ids"], loginInfo.rootIdsString) return responseHelp.buildSuccessResponseData("OK") except Exception as e: return responseHelp.buildErrorResponseData(responseHelp.kCmdServerError_DeleteError)
def appUploadFileInfo(): "上传文件信息" funcCheckStatus = lambda v: int(v) if int(v) in (defines.kFileStatusFromLocal, defines.kFileStatusFromUploading) else defines.kFileStatusFromLocal curTimestamp = unit.getTimeInt() result = checkApiParam(True, ( {"name": "cid", "checkfunc": unit.checkParamForInt}, {"name": "name", "checkfunc": lambda v: v if len(v) > 0 and len(v) <= 100 else None}, {"name": "size", "checkfunc": unit.checkParamForInt}, {"name": "type", "checkfunc": unit.checkParamForFileType}, {"name": "ext", "checkfunc": lambda v: v if len(v) <= 10 else None}, {"name": "statusForThumb", "checkfunc": funcCheckStatus, "default": defines.kFileStatusFromLocal}, {"name": "statusForScreen", "checkfunc": funcCheckStatus, "default": defines.kFileStatusFromLocal}, {"name": "createTime", "checkfunc": unit.checkParamForTimestamp, "default": curTimestamp}, {"name": "importTime", "checkfunc": unit.checkParamForTimestamp, "default": curTimestamp}, {"name": "lastModifyTime", "checkfunc": unit.checkParamForTimestamp, "default": curTimestamp}, {"name": "duration", "checkfunc": lambda v: float(v), "default": None}, {"name": "width", "checkfunc": unit.checkParamForInt, "default": None}, {"name": "height", "checkfunc": unit.checkParamForInt, "default": None}, {"name": "orientation", "checkfunc": unit.checkParamForInt, "default": None}, {"name": "longitude", "checkfunc": unit.checkParamForDouble, "default": None}, {"name": "latitude", "checkfunc": unit.checkParamForDouble, "default": None}, {"name": "memo", "checkfunc": unit.checkParamForLess1024, "default": None}, {"name": "helpInt", "checkfunc": unit.checkParamForInt, "default": None}, {"name": "helpText", "default": None},)) if not result[kParamForResult]: return result[kParamForErrorResponse] loginInfo = result[kParamForLoginInfo] param = result[kParamForRequestParams] nCatalogId = param.pop("cid") #目录信息 db = _getDbManager() catalogRow = db.getCatalogByIdAndRootIds(nCatalogId, loginInfo.rootIdsString) if not catalogRow: return responseHelp.buildErrorResponseData(responseHelp.kCmdUserError_Param) strFileName = unit.buildOriginFileName(catalogRow[dataManager.kCatalogFieldPath], param["ext"]) param["uploadUserId"] = loginInfo.userId param["fileName"] = strFileName param["statusForOrigin"] = defines.kFileStatusFromUploading param["uploadTime"] = unit.getTimeInt() nNewFileId = db.addFile(catalogRow[dataManager.kCatalogFieldRootId], nCatalogId, param) fileRow = db.getFileByIdAndRootIds(nNewFileId, None) funcForIdRelatePath = lambda : db.getCatalogIdRelatePathInfo(nCatalogId) return responseHelp.buildSuccessResponseData(dataManager.buildFileInfo(fileRow, funcForIdRelatePath))
def appUpdateFile(): "上传文件信息" funcCheckStatus = lambda v: int(v) if int(v) in (defines.kFileStatusFromLocal, defines.kFileStatusFromUploading) else defines.kFileStatusFromLocal result = checkApiParam(True, ( {"name": "id", "checkfunc": unit.checkParamForInt}, {"name": "catalogId", "checkfunc": unit.checkParamForInt, "default": None}, {"name": "name", "checkfunc": lambda v: v if len(v) > 0 and len(v) <= 100 else None, "default": None}, {"name": "ext", "checkfunc": lambda v: v if len(v) <= 10 else None, "default": None}, {"name": "size", "checkfunc": unit.checkParamForInt, "default": None}, {"name": "type", "checkfunc": unit.checkParamForFileType, "default": None}, {"name": "statusForThumb", "checkfunc": funcCheckStatus, "default": None}, {"name": "statusForScreen", "checkfunc": funcCheckStatus, "default": None}, {"name": "createTime", "checkfunc": unit.checkParamForTimestamp, "default": None}, {"name": "importTime", "checkfunc": unit.checkParamForTimestamp, "default": None}, {"name": "lastModifyTime", "checkfunc": unit.checkParamForTimestamp, "default": None}, {"name": "duration", "checkfunc": lambda v: float(v), "default": None}, {"name": "width", "checkfunc": unit.checkParamForInt, "default": None}, {"name": "height", "checkfunc": unit.checkParamForInt, "default": None}, {"name": "orientation", "checkfunc": unit.checkParamForInt, "default": None}, {"name": "longitude", "checkfunc": unit.checkParamForDouble, "default": None}, {"name": "latitude", "checkfunc": unit.checkParamForDouble, "default": None}, {"name": "memo", "checkfunc": unit.checkParamForLess1024, "default": None}, {"name": "helpInt", "checkfunc": unit.checkParamForInt, "default": None}, {"name": "helpText", "default": None},)) if not result[kParamForResult]: return result[kParamForErrorResponse] loginInfo = result[kParamForLoginInfo] param = result[kParamForRequestParams] nFileId = param.pop("id") #目录信息 unit.filterNullValue(param) bOK = False if len(param) > 0: db = _getDbManager() bOK = db.updateFile(nFileId, param, loginInfo.rootIdsString) if bOK: fileRow = db.getFileByIdAndRootIds(nFileId, loginInfo.rootIdsString) funcForIdRelatePath = lambda : db.getCatalogIdRelatePathInfo(fileRow[dataManager.kFileFieldRealCatalogId]) fileInfo = dataManager.buildFileInfo(fileRow, funcForIdRelatePath) return responseHelp.buildSuccessResponseData(fileInfo) return responseHelp.buildErrorResponseData(responseHelp.kCmdUserError_NotModify)
def appLogin(): "登陆" result = checkApiParam(False, ["userName", "password"]) if not result[kParamForResult]: return result[kParamForErrorResponse] param = result[kParamForRequestParams] db = _getDbManager() row = db.getUser(param["userName"], param["password"]) if row is None: return responseHelp.buildErrorResponseData(responseHelp.kCmdUserError_LoginNamePassword) #查询根目录信息 nUserId = row[dataManager.kUserFieldId] strUserName = row[dataManager.kUserFieldName] bOK, resp = _makeUserRootPath(nUserId) if not bOK: return resp userInfo = dataManager.buildUserInfo(row) return responseHelp.buildSuccessResponseData(userInfo)
def appGetFiles(): "获取指定目录下的文件" result = checkApiParam(True, [ {"name": "pageIndex", "checkfunc": lambda v: int(v) if int(v) >= 0 else None}, {"name": "maxPerPage", "checkfunc": lambda v: int(v) if int(v) >= 10 and int(v) <= 10000 else 100, "default": 100}, {"name": "rootIds", "checkfunc": unit.checkParamForIntList, "default": None}, {"name": "pids", "checkfunc": unit.checkParamForIntList, "default": None}, {"name": "types", "checkfunc": unit.checkParamForIntList, "default": None}, {"name": "onlySelfUpload", "checkfunc": unit.checkParamForInt, "default": 0}, {"name": "sort", "checkfunc": unit.checkParamForInt, "default": 0}]) if not result[kParamForResult]: return result[kParamForErrorResponse] loginInfo = result[kParamForLoginInfo] param = result[kParamForRequestParams] # 根目录判断 rootIds = param["rootIds"] if not rootIds or not unit.judgeIntStringsInList(rootIds, loginInfo.rootIdList): rootIds = loginInfo.rootIdsString # 上传者信息 uploadUserId = None if param["onlySelfUpload"] != 0: uploadUserId = loginInfo.userId # 查询数据 db = _getDbManager(); fileRows, pageInfo = db.getFiles(rootIds, param["pids"], param["types"], uploadUserId, param["sort"], param["pageIndex"], param["maxPerPage"]) #生成数据 ltData = dataManager.buildFileInfoList(fileRows, None) return responseHelp.buildSuccessResponseData({"list": ltData, "page": pageInfo});
def appUploadFile(): "上传文件" # print(request.args) result = responseHelp.checkApiParamWithRequestValues(True, ( {"name": "id", "checkfunc": unit.checkParamForInt}, {"name": "obp", "checkfunc": unit.checkParamForInt, "default": 0}, {"name": "tbp", "checkfunc": unit.checkParamForInt, "default": 0}, {"name": "sbp", "checkfunc": unit.checkParamForInt, "default": 0}, {"name": "cm", "default": None} ), request.args) if not result[kParamForResult]: return result[kParamForErrorResponse] loginInfo = result[kParamForLoginInfo] param = result[kParamForRequestParams] nFileId = param["id"] db = _getDbManager() fileRow = db.getFileByIdAndRootIds(nFileId, loginInfo.rootIdsString) if not fileRow: return responseHelp.buildErrorResponseData(responseHelp.kCmdUserError_Param) upFileInfos = {} orgFullFileName = None upFileNames = {} def _myUploadFileStreamFactory(aTotalContentLength, aContentType, aFileName, aContentLength): "生成文件进入写入操作" strFullFileName = None nBeginPos = 0 strDbFiledName = None if aFileName == "origin": #处理R原始文件 catalogRow = db.getCatalogByIdAndRootIds(fileRow[dataManager.kFileFieldRealCatalogId], loginInfo.rootIdsString) if catalogRow: strFullFileName = os.path.join(catalogRow[dataManager.kCatalogFieldPath], fileRow[dataManager.kFileFieldFileName]) nBeginPos = param["obp"] strDbFiledName = "statusForOrigin" nonlocal orgFullFileName orgFullFileName = strFullFileName elif aFileName == "thumb": #小缩略图 strFullFileName = unit.getFileThumbFullFileName(fileRow[dataManager.kFileFieldRealCatalogId], nFileId, 0) nBeginPos = param["tbp"] strDbFiledName = "statusForThumb" elif aFileName == "screen": #大缩略图 strFullFileName = unit.getFileThumbFullFileName(fileRow[dataManager.kFileFieldRealCatalogId], nFileId, 0) nBeginPos = param["sbp"] strDbFiledName = "statusForScreen" if not strFullFileName: raise RuntimeError("filename is error") # 判断文件的大小,与传入参数进行对比,若传入文件大小过大,则直接报错.否则根据传入参数进行写操作 nCurFileSize = os.stat(strFullFileName).st_size if os.path.isfile(strFullFileName) else 0 if nBeginPos > nCurFileSize: raise RuntimeError("begin position is error!") upFileInfos[strDbFiledName] = defines.kFileStatusFromUploaded upFileNames[aFileName] = strFullFileName # print("will open file: ", strFullFileName, "; beginPos: ", nBeginPos) f = open(strFullFileName, "a+b") f.truncate(nBeginPos) f.seek(0, os.SEEK_END) return f # 开始接收文件内容 try: stream,form,files = werkzeug.formparser.parse_form_data(request.environ, stream_factory = _myUploadFileStreamFactory, silent = False) except Exception as e: for key in upFileInfos.keys(): upFileInfos[key] = defines.kFileStatusFromUploading db.updateFile(nFileId, upFileInfos, loginInfo.rootIdsString) log.logObject().error(e) return responseHelp.buildErrorResponseData(responseHelp.kCmdUserError_Param) # 更新数据库 if orgFullFileName: upFileInfos["size"] = os.stat(orgFullFileName).st_size db.updateFile(nFileId, upFileInfos, loginInfo.rootIdsString) # 生成返回数据 fileRow = db.getFileByIdAndRootIds(nFileId, loginInfo.rootIdsString) funcForIdRelatePath = lambda : db.getCatalogIdRelatePathInfo(fileRow[dataManager.kFileFieldRealCatalogId]) fileInfo = dataManager.buildFileInfo(fileRow, funcForIdRelatePath) result = {"fileInfo": fileInfo} #写入检验值 strCm = param["cm"] if strCm: strCm = strCm.lower() bSha1 = strCm == "sha1" bMd5 = not bSha1 and strCm == "md5" if bSha1 or bMd5: check = {} for key, filename in upFileNames.items(): check[key] = unit.SHA1FileWithName(filename) if bSha1 else unit.MD5FileWithName(filename) result["check"] = check return responseHelp.buildSuccessResponseData(result)