def getCatalogsByParentIds(self, aParentIds, aLimitStrRootIds): "获取在RootIds下的所有aParentIds数据" return self.select(_kCatalogTableName, { formatInField("rootId", aLimitStrRootIds): None, formatInField("parentId", aParentIds): None }, aOneRecord=False)
def deleteFiles(self, aIds, aLimitStrRootIds): "删除指定的文件" where = { formatInField("id", aIds): None, formatInField("rootCatalogId", aLimitStrRootIds): None } rows = self.select(_kFileTableName, where, aOneRecord=False) self._defFilesRes(rows) self.delete(_kFileTableName, where)
def deleteCatalogs(self, aDelCids, aLimitStrRootIds): "删除指定目录" if aDelCids == None or aLimitStrRootIds == None: return #递归查询所有将被删除的目录信息 sql = """ with recursive cids(x) AS( select id from {table} where id in ({ids}) and rootId in ({limitIds}) union all select id from {table}, cids where {table}.parentid = cids.x ) select * from {table} where {table}.id in cids """.format(table=_kCatalogTableName, ids=aDelCids, limitIds=aLimitStrRootIds) rows = self.fetch(sql, fetchone=False) if not rows or len(rows) == 0: return waitDeletePaths = [] for item in rows: s = item[kCatalogFieldPath] waitDeletePaths.append(s) #移动目录下的所有挂接到别的目录的子目录到实际位置 self._copySubCatalogToRealPath(waitDeletePaths) #组合所有目录id cids = unit.buildFormatString(rows, kCatalogFieldId) #删除所有挂接在目录下的所有文件的缩略图 self._clearAllBuildFiles(cids) #所有挂接到此目录下,但实际位置在其它目录的文件,删除之 self._queryWaitDeleteFiles(cids, waitDeletePaths) #所有实际在此目录,但已经挂接到其它目录下的,移动文件并修改realCatalogId self._moveFilesToNewPath(cids) #删除记录 self.delete(_kCatalogTableName, {formatInField("id", cids): None}) self.delete(_kFileTableName, {formatInField("catalogId", cids): None}) self.delete(_kUserAssociateTableName, {formatInField("rootCatalogId", cids): None}) #删除文件 unit.removePath(waitDeletePaths)
def _queryWaitDeleteFiles(self, aCids, aToList): deleteFileRows = self.select( _kFileTableName, { formatInField("catalogId", aCids): None, formatNotInField("realCatalogId", aCids): None }, aOneRecord=False) if deleteFileRows and len(deleteFileRows) > 0: self._defFilesRes(deleteFileRows, 1, aToList)
def getCatalogIdRelatePathInfo(self, aStrIds): "获取指定id所对应的路径信息,组成 [id] = path 的格式" rows = self.select(_kCatalogTableName, {formatInField("id", aStrIds): None}, "id, path", False) result = {} for item in rows: result[item[0]] = item[1] return result
def getFileByIdAndRootIds(self, aFileId, aLimitStrRootIds): """获取指定 Id 的文件信息 :aFileId: 文件 Id :aLimitStrRootIds: 根目录IDs """ return self.select( _kFileTableName, { "id": aFileId, formatInField("rootCatalogId", aLimitStrRootIds): None })
def getFileByRealCatalogId(self, aRealCatalogId, aFileName, aLimitStrRootIds=None): "根据指定目录 ID 和文件名获取文件信息" return self.select( _kFileTableName, { "realCatalogId": aRealCatalogId, "fileName": aFileName, formatInField("rootCatalogId", aLimitStrRootIds): None })
def updateCatalog(self, aId, aCatalogInfo, aLimitStrRootIds): "更新目录信息, 若需要修改 parentId, 则可能需要修改rootId和所对应的File的rootCatalogId" bUpdateFileTable = False nNewRootId = None if aLimitStrRootIds != None: # 只有传递 rootids 信息时,才进行修改(客户端才需要传递) nNewParentId = aCatalogInfo.get("parentId") if nNewParentId != None and nNewParentId > 0: sql = """select a.rootId as newRootId, b.rootId as curRootid, b.parentId from {table} a, {table} b where a.id = ? and a.rootid in ({limitIds}) and b.id = ? and b.rootid in ({limitIds}) """.format(table=_kCatalogTableName, limitIds=aLimitStrRootIds) # print(sql) row = self.fetch(sql, (nNewParentId, aId)) if not row: return False # 判断是否更新 if nNewParentId == row[2]: aCatalogInfo.pop("parentId") elif row[0] != row[1]: bUpdateFileTable = True nNewRootId = row[0] aCatalogInfo["rootId"] = nNewRootId else: aCatalogInfo.pop("parentId", None) else: # 若没有rootIds,则不对parentId进行处理 aCatalogInfo.pop("parentId", None) #更新Catalog表 afv = None if not aCatalogInfo.get("lastModifyTime"): afv = {"lastModifyTime": unit.getTimeInt()} bOK = self.update(_kCatalogTableName, { "id": aId, formatInField("rootId", aLimitStrRootIds): None }, aCatalogInfo, afv) #更新File表 if bOK and bUpdateFileTable: bOK = self.update(_kFileTableName, {"realCatalogId": aId}, {"rootCatalogId": nNewRootId}) return bOK
def _moveFilesToNewPath(self, aCids): moveFileRows = self.select(_kFileTableName, { formatInField("realCatalogId", aCids): None, formatNotInField("catalogId", aCids): None }, aOneRecord=False) if moveFileRows and len(moveFileRows) > 0: srcIds = unit.buildFormatString(moveFileRows, kFileFieldRealCatalogId) destIds = unit.buildFormatString(moveFileRows, kFileFieldCatalogId) srcInfo = self.getCatalogIdRelatePathInfo(srcIds) destInfo = self.getCatalogIdRelatePathInfo(destIds) for item in moveFileRows: newInfo = {"realCatalogId": item[kFileFieldCatalogId]} newPath = destInfo[item[kFileFieldCatalogId]] srcPath = srcInfo[item[kFileFieldRealCatalogId]] srcName = item[kFileFieldFileName] srcFile = os.path.join(srcPath, srcName) if not unit.moveFile(srcFile, newPath): if not item[kFileFieldName]: newInfo["name"] = srcName i = 1 newRenameFile = None while True: srcTempName = srcName ls = srcTempName.split(".") if ls and len(ls) > 1: ls[-2] = ls[-2] + "_{}".format(i) srcTempName = unit.buildFormatString(ls, 0, aSpace=".") else: srcTempName += "_{}".format(i) i += 1 newRenameFile = os.path.join(newPath, srcTempName) if not os.path.exists(newRenameFile): if unit.moveFile(srcFile, newRenameFile): newInfo["filename"] = srcTempName break #end while #end if self.update(_kFileTableName, {"id": item[kFileFieldId]}, newInfo)
def updateFile(self, aFileId, aFileInfo, aLimitStrRootIds): """更新文件信息 若存在aLimitStrRootIds和aFileInfo["catalogId"]有效时, 只修改数据信息,不会真正移动文件 """ if aLimitStrRootIds != None and aFileInfo.get("catalogId") != None: #新位置是否有效s nNewCatalogId = aFileInfo["catalogId"] sql = """select a.rootId as newRootId, b.catalogId as curCatalogId, b.rootCatalogId as curRootId from {catalogTable} a, {fileTable} b where a.id = ? and a.rootId in ({limitIds}) and b.id = ? and b.rootCatalogid in ({limitIds}) """.format(catalogTable=_kCatalogTableName, fileTable=_kFileTableName, limitIds=aLimitStrRootIds) row = self.fetch(sql, (nNewCatalogId, aFileId)) if not row: return False #是否要修改 catalogId nNewRootId = row[0] nCurCatalogId = row[1] nCurRootId = row[2] if nNewRootId != nCurRootId: aFileInfo["rootCatalogId"] = nNewRootId if nNewCatalogId == nCurCatalogId: aFileInfo.pop("catalogId") # 更新数据 afv = None if aFileInfo.get("lastModifyTime") == None: afv = {"lastModifyTime": unit.getTimeInt()} return self.update( _kFileTableName, { "id": aFileId, formatInField("rootCatalogId", aLimitStrRootIds): None }, aFileInfo, afv)
def getFiles(self, aRootIds, aPids, aTypes, aUploadUserId, aSort, aPageIndex, aMaxPerPage): """查找指定路径下的文件内容, 对应API: file.icc :aRootIds: 根目录Id, 如: 1,2,3 :aPids: 所属目录ID, 可能为空 :aTypes: 文件的类型 :aUploadUserId: 上传用户Id, None表示不限定 :aSort: 排序的方式: 1->文件创建时间, 2->上传时间, 3->文件大小, 4->持续时间, 5->文件尺寸 >0表示升序, <0表示降序 :aPageIndex: 第几页 :aMaxPerPage: 每页最大数量 :returns: (datelist, pageInfo) """ funcNotEqual = lambda v: lambda: "{} != {}".format( v, defines.kFileStatusFromUploading) where = { formatInField("rootCatalogId", aRootIds): None, funcNotEqual("statusForOrigin"): None, funcNotEqual("statusForThumb"): None, funcNotEqual("statusForScreen"): None } if aPids and len(aPids) > 0: where[formatInField("catalogId", aPids)] = None if aTypes and len(aTypes) > 0: where[formatInField("type", aTypes)] = None if aUploadUserId: where[lambda: "uploadUserId = {}".format(aUploadUserId)] = None strWhere = self.FormatFieldValues(where, None, "and") if aSort != None and aSort != 0: nAbsSort = aSort if aSort > 0 else -aSort if nAbsSort == 1: strSortFieldName = "createTime" elif nAbsSort == 2: strSortFieldName = "uploadTime" elif nAbsSort == 3: strSortFieldName = "size" elif nAbsSort == 4: strSortFieldName = "duration" elif nAbsSort == 5: strSortFieldName = "width {orderMethod}, height" else: return None, None strWhere += " order by " + strSortFieldName + " {orderMethod}" strWhere = strWhere.format( orderMethod="asc" if aSort > 0 else "desc") nLimitCount = aMaxPerPage if aMaxPerPage > 0 else 100 nLimitBegin = aPageIndex * nLimitCount #查询内容 strWhere += " limit {begin}, {count}".format(begin=nLimitBegin, count=nLimitCount) sql = "select * from {} where {}".format(_kFileTableName, strWhere) # print(sql) fileInfos = self.fetch(sql, fetchone=False) #分页信息 dbCount = self.select(_kFileTableName, where, aFields="count(1)") nItemCount = dbCount[0] if dbCount else 0 return fileInfos, { "pageIndex": aPageIndex, "maxPerPage": nLimitCount, "pageCount": (nItemCount + nLimitCount - 1) // nLimitCount }
def _clearAllBuildFiles(self, aCids): rows = self.select(_kFileTableName, {formatInField("catalogId", aCids): None}, aOneRecord=False) if rows and len(rows) > 0: self._defFilesRes(rows, 2)
def getCatalogByIdAndRootIds(self, aId, aLimitStrRootIds): "获取指定 Id 的目录信息" return self.select(_kCatalogTableName, { "id": aId, formatInField("rootId", aLimitStrRootIds): None })