def GET(self, *arg, **params): if not arg: raise cherrypy.HTTPError(460, 'Bad Parameter') if arg[0] in ['date','folders','tags']: if len(arg) > 1: return self._getGroupFiles(arg[0],arg[1:], params) else: return self._getImageGroup(arg[0], params) elif arg[0] == 'thumbnail': if len(arg) < 2: raise cherrypy.HTTPError(460, 'Bad Parameter') return self._getThumbImage(UtilFunc.getPictureHashPath(''.join(arg[1:])), '.jpg') elif arg[0] == 'create': if len(arg) < 2: raise cherrypy.HTTPError(460, 'Bad Parameter') path = UtilFunc.formatPath(arg[1:]) size = int(params.get('size', 170)) (tempThumbImage,ext) = thumbnail.getThumbNailImage(path,size) return self._getThumbImage(tempThumbImage, ext) elif arg[0] == 'all': ret = UtilFunc.formatPhotoRet(SqliteFunc.execFileCacheTags(), params) return WebFunc.jsonResult({'photos': ret}) else: return self._getAutoUploads(arg[0])
def _getGroupFiles(self, groupType, groupValue, params): if groupType == 'tags': files = SqliteFunc.execFileCacheTags(' and a.tag = "%s"'%groupValue[0], False, params = params) elif groupType == 'folders': folder = UtilFunc.formatPath(groupValue) if not os.path.isdir(folder): raise cherrypy.HTTPError(460, 'Bad Parameter') folders, photos = UtilFunc.walkFolder(folder, 'picture', params) return WebFunc.jsonResult({'folders':folders,'photos':photos}) elif groupType == 'date': files = SqliteFunc.execFileCacheTags(' and a.groupTime = "%s"'%groupValue[0], params = params) else: raise cherrypy.HTTPError(460, 'Bad Parameter') return WebFunc.jsonResult({'photos':UtilFunc.formatPhotoRet(files, {})})
def _searchImages(self, params): start = int(params.get('offset', 0)) limit = int(params.get('limit', -1)) name = params.get('key', None) if not name or start < 0: raise cherrypy.HTTPError(460, 'Bad Parameter') files = SqliteFunc.execFileCacheTags(' and a.name like '%" + name + "%'') if limit == -1: file_info = files[start:] finished = 1 elif int(start) >= 0 and int(start+limit)<len(files): file_info= files[start:(start+limit)] finished = 0 elif int(start) >=0 and (start+limit) >= len(files): file_info = files[start:len(files)] finished = 1 ret = UtilFunc.formatPhotoRet(file_info, {'order':params.get('order',None)}) return WebFunc.jsonResult({'photos':ret,'finished':finished})
def walkFolder(folder, type='picture', params={}): folders, files = [], [] for filename in os.listdir(folder): if isWindowsSystem(): filename = filename.decode('gbk') subfolder = os.path.join(folder, filename).replace('\\', '/').replace('//', '/') if os.path.isdir(subfolder): info = {'name': filename, 'url': subfolder} folders.append(info) else: continue folder = folder.decode('utf8').replace('\\', '/').replace('//', '/') if type in ['music', 'video']: # sqlStr = 'select * from fileCache where fileType = ? and folder = ?' # ret = ProfileFunc.execSubLibrarySqlbyPath(folder, sqlStr, (type,folder,)) ret = SqliteFunc.tableSelect(SqliteFunc.TB_CACHE, [], 'fileType = ? and folder = ?', ( type, folder, )) files = formatMediaRet(ret, {'order': params.get('order', None)}) elif type == 'picture': ret = SqliteFunc.execFileCacheTags(' and a.folder = "%s"' % folder) files = formatPhotoRet(ret, {'order': params.get('order', None)}) start = int(params.get('offset', 0)) limit = int(params.get('limit', -1)) if int(limit) != -1: folders = folders[start:(start + limit)] files = files[start:(start + limit)] else: folders = folders[start:] files = files[start:] return folders, files
def _getImageGroup(self, groupType, params): orderBy = params.get('order', None) limit = int(params.get('limit',1)) groupDict = {'date':'groupTime','folders':'folders','tags':'tag'} if not groupType in groupDict.keys(): raise cherrypy.HTTPError(460,'Bad Parameter') group_name = groupDict[groupType] scan_list_p = [] if group_name == 'folders' and not ProfileFunc.getAllMediaFolders(): raise cherrypy.HTTPError(464, 'Not Exist') if groupType == 'tags': #sqlStr = 'select tag, count(*) from (select * from fileCache where fileType="picture") group by tag' #groups = ProfileFunc.execAllScanFolderSql(sqlStr) sqlStr = 'select tag, count(*) from (select * from tags where fileType="picture") group by tag' groups = SqliteFunc.execSql(sqlStr) return WebFunc.jsonResult({'tags':[{'name':file['tag'],'total':file['count(*)']} for file in groups]}) elif groupType == 'folders': SqliteFunc.tableRemove(SqliteFunc.TB_CACHE, "folder is ''") scan_list= ProfileFunc.getAllSetFolders() for path_ele in set(scan_list): if not UtilFunc.is_sub_folder(path_ele, scan_list): scan_list_p.append(path_ele) ret_list_info = [] for ret_path in scan_list_p: if os.path.exists(ret_path): ret_list_info.append(ProfileFunc.dir_info(ret_path)) else: SqliteFunc.tableRemove(SqliteFunc.TB_CACHE, 'url=?', (ret_path,)) return WebFunc.jsonResult({'groupType' : group_name, 'folders' : UtilFunc.formatPhotoRet(ret_list_info, params)}) else: start = params.get('start', '1970-01') end = params.get('end', '9999-12') baseStr = 'select * from fileCache where fileType="picture" and groupTime >= "%s" and groupTime <="%s"'%(start,end) sqlStr = 'select groupTime, count(*) from (%s) group by groupTime order by lastModify asc'%baseStr #groups = SqliteFunc.execSql(sqlStr) groups = SqliteFunc.execSql(sqlStr) if not groups: for disk in ProfileFunc.GetBoxDisks(): if ProfileFunc.getMainServer().diskState.has_key(disk) and \ ProfileFunc.getMainServer().diskState[disk] == 2: raise cherrypy.HTTPError(463, 'Not Permitted') if UtilFunc.isLowDiskSpace(disk): raise cherrypy.HTTPError(467, 'Not Enough Disk Space') ret, ret_count, tmp_ret_dict = [], 0, {} for fileInfo in groups: if not tmp_ret_dict.has_key(fileInfo[group_name]): tmp_ret_dict[fileInfo[group_name]] = ret_count else: ret[tmp_ret_dict[fileInfo[group_name]]]['total'] += fileInfo['count(*)'] continue ret_count += 1 dbList = SqliteFunc.execFileCacheTags(' and a.%s = "%s"'%(group_name, fileInfo[group_name])) photos = UtilFunc.formatPhotoRet(dbList, params) if not photos: continue ret.append({'name' : fileInfo[group_name], 'total' : fileInfo['count(*)'], 'photos': photos}) if orderBy: cmpInfo = UtilFunc.httpArgToCmpInfo(orderBy) if len(cmpInfo) > 0 : ret.sort(lambda x,y: UtilFunc.dictInfoCmp(x, y, cmpInfo)) return WebFunc.jsonResult({groupType:ret})