def index(self, *arg, **params): try: if params.get('storages'): storages = params.get('storages') elif params.get('disk') and not UtilFunc.isWindowsSystem(): storages = params.get('disk') storages = ProfileFunc.get_name_label(storages) else: storages = ProfileFunc.GetBoxDisks(False)[0] global file_dir file_dir = storages tmpl = env.get_template('popoCloud.html') s_stores = [] list_files = [] extInfo = {} extInfo['limit'] = 1000 extInfo = UtilFunc.getOptParamsInfo(extInfo) if os.path.exists(storages): for file_info in UtilFunc.getFileList(storages, extInfo): list_files.append({"path":file_info.get('url').split("/")[-1],"size":file_info.get("contentLength"),"modify":file_info.get("lastModify")}) if UtilFunc.isWindowsSystem(): if ProfileFunc.GetBoxDisks(False): names = [u'我的磁盘一'] else: for s_store in ProfileFunc.GetBoxDisks(False): s_stores.append(ProfileFunc.get_label_name(s_store)) names = s_stores return tmpl.render(names=names, list_files = list_files) except Exception,e: Log.exception('index Failed! Reason[%s]'%e)
def getDefaultPath(path=None): if path: if not getDiskPath(path): return os.path.join( ProfileFunc.GetBoxDisks(False)[0], path[1:] if path.startswith('/') else path) return path else: return ProfileFunc.GetBoxDisks(False)[0]
def getContactBackupDir(): disks = ProfileFunc.GetBoxDisks() if not disks: return None filePath = os.path.join(disks[0], '.popoCloud') folder = os.path.join(filePath, 'Contact Backup').replace('\\', '/') return folder
def POST(self, *arg, **params): if not ProfileFunc.GetBoxDisks(): raise cherrypy.HTTPError(465, 'Not Exist Disk') intent = params.get('intent', 'set') if intent == 'set': return self._setlabel(params) return
def getRealPathByName(name): num = 1 for disk in ProfileFunc.GetBoxDisks(): diskname = u'Storage' + str(num) if diskname in name: return disk num += 1 return None
def formatAutoUploadPath(parentFolder): disk = ProfileFunc.GetBoxDisks() if not disk: return None disk_path = os.path.join(disk[0], "Camera Uploads") if not os.path.exists(disk_path): os.mkdir(disk_path) parentFolder = parentFolder.replace( parentFolder.split("Camera Uploads")[0], disk[0] + "/") return parentFolder
def createfile(self, *arg, **params): global file_dir if not file_dir: file_dir = ProfileFunc.GetBoxDisks(False)[0] if params.get("docname"): file_path = os.path.join(file_dir,params.get("docname")) createFile(file_path,False,{'size':1}) return self.index(storages=file_dir)
def GET(self, *arg, **params): if arg: storageId = arg[0] if storageId == 'all': files = UtilFunc.getFileList(ProfileFunc.GetBoxDisks(), UtilFunc.getOptParamsInfo(params)) return WebFunc.jsonResult({'total':len(files),'files':files}) diskpath = ProfileFunc.getDiskById(storageId) if not diskpath: raise cherrypy.HTTPError(460, 'Bad Parameters') if 'mediaFolders' in arg: ret = ProfileFunc.getMainServer().scanFolderMoniter.getMediaFolder(params) return WebFunc.jsonResult(ret) else: return WebFunc.jsonResult(self._getStorageInfo(diskpath)) else: storages = [] for disk in ProfileFunc.GetBoxDisks(False): storages.append(self._getStorageInfo(disk)) return WebFunc.jsonResult({'storages':storages, 'status':ProfileFunc.getDiskStatus()})
def _getKeyParameter(self, params, key, default=None): if not ProfileFunc.GetBoxDisks(): raise cherrypy.HTTPError(465, 'Not Exist Disk') intent = params.get(key, None) if not intent: if default: return default raise cherrypy.HTTPError(460, 'Bad Parameter') return intent
def make_file(self, binary=None): """Overridable: return a readable & writable file. The file will be used as follows: - data is written to it - seek(0) - data is read from it The 'binary' argument is unused -- the file is always opened in binary mode. This version opens a temporary file for reading and writing, and immediately deletes (unlinks) it. The trick (on Unix!) is that the file can still be used, but it can't be opened by another process, and it will automatically be deleted when it is closed or when the current process terminates. If you want a more permanent file, you derive a class which overrides this method. If you want a visible temporary file that is nevertheless automatically deleted when the script terminates, try defining a __del__ method in a derived class which unlinks the temporary files you have created. """ import tempfile import ProfileFunc if self.parent and 'path' in self.parent: fullpath = unicode( ProfileFunc.slashFormat(self.parent['path'].value)) dname, fname = os.path.split(fullpath) if not os.path.exists(dname) and ProfileFunc.isPathValid(dname): os.makedirs(dname) #Need check the folder 'dname' is exist? if dname and fname: f = tempfile.NamedTemporaryFile('w+b', dir=dname, prefix='.popotmp_', delete=False) #f = file(fullpath, 'w+b') else: f = tempfile.NamedTemporaryFile('w+b', prefix=tmp_prefix, delete=True) return f else: try: #TODO: should not always use first disk/partition!!! dir = ProfileFunc.GetBoxDisks()[0] + "/.popoCloud/tmp" if not os.path.exists(dir) and ProfileFunc.isPathValid(dir): os.makedirs(dir) flag = False except: dir = None return tempfile.NamedTemporaryFile(dir=dir, delete=False)
def POST(self, *arg, **params): cherrypy.response.timeout = 3600 * 24 cherrypy.response.headers["Access-Control-Allow-Origin"] = "*" if not ProfileFunc.GetBoxDisks(): Log.error("ContactFileUpload Failed!") raise cherrypy.HTTPError(465, 'Not Exist Disk') lcHDRS = { key.lower(): val for key, val in cherrypy.request.headers.iteritems() } tmp_file_path = None tmp_file = None filepath = params.get("filePath", None) if not filepath: raise cherrypy.HTTPError(460, 'Bad Parameter') filepath = unicode( urllib.unquote(filepath.replace("\\", "/").strip()).encode('utf-8')) contact_dir = getContactBackupDir() path = os.path.join(contact_dir, filepath).replace('\\', '/') if UtilFunc.isLinuxDiskReadOnly(path): raise cherrypy.HTTPError(463, 'Not Permitted') data_length = int(lcHDRS.get("content-length", 0)) free, capacity = UtilFunc.getRemainSpace(path) if free < data_length / 1024: raise cherrypy.HTTPError(467, 'Not Enough Disk Space') try: (parentFolder, filename) = os.path.split(path) if not os.path.exists(path): UtilFunc.makeDirs(parentFolder) replaceOldFile = UtilFunc.toBoolean( lcHDRS.get('replaceoldfile', True)) tmp_file_path = os.path.join( parentFolder, ".popotmp_" + str(time.time() * 1000)[-8:]).replace('\\', '/') tmp_file = open(tmp_file_path, "wb") tmp_length = 0 data = cherrypy.request.body.fp while True: file_data = data.read(8192) if not file_data: break tmp_length += len(file_data) tmp_file.write(file_data) tmp_file.close() except Exception, e: Log.error("ContactFileUpload Failed! Reason[%s]" % e) if tmp_file: tmp_file.close() os.remove(tmp_file_path) raise cherrypy.HTTPError(462, 'Operation Failed')
def isLinuxDiskReadOnly(path): if isLinuxSystem(): try: path = getDiskPath(path) if not path: path = ProfileFunc.GetBoxDisks()[0] file_path = os.path.join(path, ".popo_ReadOnly") file(file_path, "wb").close() os.remove(file_path) except Exception, e: Err_msg = "Check Disk[%s] State Except! Info:[%s]" % (path, e) Log.warning(Err_msg) #if "[Errno 30] Read-only file system" in Err_msg: return True
def getStorageName(path): num = 1 if UtilFunc.isLinuxSystem(): path = UtilFunc.getDiskPath(path) if path == '/mnt/popoCloud': return 'Storage1' key = 'Storage' else: key = 'Folder' for disk in ProfileFunc.GetBoxDisks(False): if path == disk: return key+str(num) num += 1 return None
def _getLibraryGroupByType(self, params): typeName = params.get('fileType', None) orderBy = params.get('orderBy', None) if not typeName or not typeName in (PopoConfig.filters.keys() + ['other']): raise cherrypy.HTTPError(460, 'Bad Parameter') if not ProfileFunc.GetBoxDisks(): raise cherrypy.HTTPError(465, 'Not Exist Disk') datas = SqliteFunc.tableSelect(SqliteFunc.TB_CACHE, [], 'fileType= ?', (typeName, ), params) return WebFunc.jsonResult({ 'fileType': typeName, 'datas': UtilFunc.formatFilesRet(datas, {}) })
def GET(self, *arg, **params): if not ProfileFunc.GetBoxDisks(): raise cherrypy.HTTPError(465, 'Not Exist Disk') backup_dir = getContactBackupDir() if not os.path.exists(backup_dir): raise cherrypy.HTTPError(464, 'Not Exist') intent = params.get('intent', 'all') if intent == 'count': return self._getContactCount(arg, params) elif intent == 'contact': return self._getOneContact(arg, params) elif intent == 'all': return self._getAllContacts(arg)
def POST(self, *arg, **params): if not arg: raise cherrypy.HTTPError(460, 'Bad Parameters') storageId = arg[0] if not ProfileFunc.GetBoxDisks() or not storageId in ProfileFunc.getStorageIds(): raise cherrypy.HTTPError(465, 'Not Exist Disk') folders, delpaths = params.get('folders',[]), params.get('except',[]) if not folders and not delpaths: raise cherrypy.HTTPError(460, 'Bad Parameter') folders = UtilFunc.strToList(folders) delpaths = UtilFunc.strToList(delpaths) ret = ProfileFunc.getMainServer().scanFolderMoniter.setMediaFolder(folders,delpaths) cherrypy.response.status = 205 return WebFunc.jsonResult(ret)
def initDBTables(): dropDBTable(TB_CACHE) createDBTables() if UtilFunc.isPCMode() and not tableSelect(TB_FOLDER): path = ProfileFunc.GetPictureFolder() tableInsert(TB_FOLDER, ['type', 'partpath', 'path', 'scanning', 'needScan'], ['all', UtilFunc.getDiskPath(path), path, '0', '1']) for disk in ProfileFunc.GetBoxDisks(): did, fs, name = Command.getDiskInfo(disk) path = UtilFunc.getDiskPath(disk, True) ProfileFunc._fileService.diskInfo[path] = { 'id': did, 'fs': fs, 'name': name }
def __init__(self, _searchInfo, _keyword, _folder, _recursive): threading.Thread.__init__(self) self.searchInfo = _searchInfo self.ret = [] self.keyword = _keyword self.recursive = _recursive if UtilFunc.isWindowsSystem() and UtilFunc.isSlash(_folder): rootDirs = ProfileFunc.getRootFolders() rootDirs = UtilFunc.mergeSubFolder(rootDirs, 'url') self.folders = [] for dirInfo in rootDirs: self.folders.append(dirInfo['url']) else: if PopoConfig.Hardware == "1.5" and _folder == '/': self.folders = ProfileFunc.GetBoxDisks() else: self.folders = [_folder]
def getDiskPath(path, flag=False): if isWindowsSystem() and os.path.exists(path): args = path.split(':') if args: return args[0] + ':/' else: return None elif isDarwinSystem() and os.path.exists(path): if flag: return getAppDataFolder() return '/' + path.split('/')[1] if path: disks = ProfileFunc.GetBoxDisks() if disks: if isLinuxSystem(): disks.append("/mnt/popoCloud") for disk in disks: disk = disk.replace('\\', '/') if disk in path: return disk return None
def DownloadFile(filePath): if not ProfileFunc.GetBoxDisks(): raise cherrypy.HTTPError(464, 'Not Exist') filePath = ProfileFunc.slashFormat(filePath) if UtilFunc.isShorcut(filePath): filePath = UtilFunc.getShortcutRealPath(filePath) filePathShort = UtilFunc.getShortPath(filePath) if os.path.isdir(filePathShort): raise cherrypy.HTTPError(460, 'Bad Parameter') elif os.path.exists(filePathShort): filename = os.path.basename(filePath) filename = UtilFunc.StringUrlEncode(filename) return PostStatic.serve_download(filePathShort, filename) else: raise cherrypy.HTTPError(464, 'Not Exist')
def download_file(self, *arg, **params): global file_dir zip_file = os.path.join(ProfileFunc.GetBoxDisks(False)[0], 'zip_file.zip') if os.path.exists(zip_file): os.remove(zip_file) file_names= params.get('names') file_names = file_names.split(',') file_paths = [] for file_name in file_names: file_paths.append(os.path.join(file_dir, file_name)) try: zip = zipfile.ZipFile(zip_file,'w',zipfile.ZIP_DEFLATED) for filename in file_paths: zip.write(filename) zip.close() except Exception,e: Log.exception('rename Failed! Reason[%s]'%e)
def start(self): self.status = STATUS_WORK for disk in ProfileFunc.GetBoxDisks(False): # folders = ProfileFunc.execConfDbSqlbyPath(disk, 'select url, type from mediafolder') # if not folders: # if UtilFunc.isWindowsSystem(): # picFolder = ProfileFunc.GetPictureFolder() # if self.folders.has_key(disk): continue # self._initMediaFolder(disk, [{'folder':picFolder,'type':'all'}], STATUS_IDLE, True) # else: # self._initMediaFolder(disk, [{'folder':disk,'type':'all'}], STATUS_IDLE, True) # else: # scanFolders = [{'folder':disk + folderInfo['url'].replace('\\','/').replace('//','/'),'type':folderInfo['type']} for folderInfo in folders] # self._initMediaFolder(disk, scanFolders, STATUS_IDLE, False) self._initMediaFolder(disk, [{ 'folder': disk, 'type': 'all' }], STATUS_IDLE, True) thread.start_new_thread(self.scanMoniter, ())
def POST(self, **params): action = params.get('action', '') files = params.get('files', []) onExist = params.get('onExist', 'rename') if not action or not files: raise cherrypy.HTTPError(460, 'Bad Parameters') files = UtilFunc.strToList(files) if not ProfileFunc.GetBoxDisks(): raise cherrypy.HTTPError(465, 'Not Exist Disk') target = params.get('target', '') if action != 'delete': target = params.get('target', '') if not target: Log.error("Operate File Failed, target is None!!!!") raise cherrypy.HTTPError(460, 'Bad Parameters') target = ProfileFunc.slashFormat(target) if UtilFunc.isLinuxSystem(): if UtilFunc.isLinuxDiskReadOnly(target): raise cherrypy.HTTPError(463, 'Not Permitted') if UtilFunc.isLowDiskSpace(target): raise cherrypy.HTTPError(467, 'Not Enough Disk Space') info = {} info['totalFiles'] = len(files) info['successFiles'] = 0 info['finishedFiles'] = 0 info['skipedFiles'] = 0 info['error'] = [] info['finished'] = 0 uuidStr = str(uuid.uuid4()) self.operateMap[uuidStr] = info thread = BatchThread(info, files, action, target, onExist) thread.start() cherrypy.response.status = 202 return WebFunc.jsonResult({"id": uuidStr})
def POST(self, *arg, **params): filetype = '.'.join(arg) folder = params.get('path', '/') keyword = params.get('term', None) if not ProfileFunc.GetBoxDisks(): raise cherrypy.HTTPError(465, 'Not Exist Disk') if not folder or not keyword: raise cherrypy.HTTPError(460, 'Bad Parameters') if filetype and filetype in PopoConfig.filters.keys(): #sqlStr = 'Select * from filecache where name like "%s" and fileType = "%s"'%('%' + keyword + '%', filetype,) datas = SqliteFunc.tableSelect(SqliteFunc.TB_CACHE, [], 'name like ? and fileType = ?', ( '%' + keyword + '%', filetype, )) return WebFunc.jsonResult( {'files': UtilFunc.formatFilesRet(datas, params)}) recursive = UtilFunc.toBoolean(params.get('recursive', True)) if UtilFunc.isSlash(folder): path = ProfileFunc.GetBoxDisks()[0] else: path = folder #folder = ProfileFunc.slashFormat(folder) if not os.path.exists(folder): raise cherrypy.HTTPError(464, 'Not Exist') if UtilFunc.isLinuxSystem(): if UtilFunc.isLinuxDiskReadOnly(path): raise cherrypy.HTTPError(463, 'Not Permitted') if UtilFunc.isLowDiskSpace(path): raise cherrypy.HTTPError(467, 'Not Enough Disk Space') try: searchInfo = {} searchInfo['path'] = '' searchInfo['folder'] = '' searchInfo['folderCount'] = 0 searchInfo['fileCount'] = 0 searchInfo['totalSize'] = 0 searchInfo['finished'] = 0 searchInfo['start'] = 0 searchInfo['startTime'] = int(time.time()) searchInfo['lastTime'] = int(time.time()) searchInfo['success'] = 0 Log.debug('Start Search!!!! folder[%s]' % folder) uuidStr = None while True: uuidStr = str(uuid.uuid4()) if uuidStr not in self.searchMap: ProfileFunc.initSearchDB(uuidStr) searchInfo['id'] = uuidStr thread = SearchThread(searchInfo, keyword, folder, recursive) statusThread = SearchStatusThread(searchInfo, self.searchMap) self.searchMap[uuidStr] = { 'thread': thread, 'statusThread': statusThread, 'info': searchInfo } thread.start() statusThread.start() break cherrypy.response.status = 202 return WebFunc.jsonResult({"id": uuidStr}) except Exception, e: Log.error("Search Failed! Reason[%s]" % e) raise cherrypy.HTTPError(462, 'Operation Failed')
return True else: raise cherrypy.HTTPError(401) elif tokenType == 'Token': clientId, token = base64.decodestring(tokenStr).split(':') else: raise cherrypy.HTTPError(401) except Exception, e: Log.exception('CheckAuth Exception! Reason[%s]' % e) raise cherrypy.HTTPError(401) if not UtilFunc.toBoolean(verifyToken(token, clientId)): raise cherrypy.HTTPError(401) if not cherrypy.request.script_name in NO_CHECKDISK_API and not ProfileFunc.GetBoxDisks( ): raise cherrypy.HTTPError(465, 'Not Exist Disk') return True cherrypy.tools.auth = cherrypy.Tool('before_handler', checkAuth) def jsonResult(ret): if ret == None: return callBack('{}') if isinstance(ret, types.DictType): return callBack(json.dumps(ret))
def getMediaFolder(self, params): folder = params.get('folder', '') orderBy = params.get('orderBy', 'isFolder desc') fileType = params.get('fileType', None) limit = int(params.get('limit', -1)) offset = int(params.get('offset', 0)) if fileType and not fileType in PopoConfig.filters.keys(): raise cherrypy.HTTPError(460, 'Bad Parameter') if not ProfileFunc.GetBoxDisks(): raise cherrypy.HTTPError(465, 'Not Exist Disk') if not folder: filelist = ProfileFunc.GetBoxDisks() else: if UtilFunc.isWindowsSystem(): folder = UtilFunc.formatPath(folder) if not os.path.exists(folder): raise cherrypy.HTTPError(464, 'Not Exist') if os.path.isfile(folder): raise cherrypy.HTTPError(460, 'Bad Parameter') filelist = os.listdir(folder) result = ProfileFunc.execConfDbSqlbyPath( folder, 'select url, type from mediafolder', None) if not result: selFolders = {} else: selFolders = {(UtilFunc.getDiskPath(folder) + UtilFunc.toLinuxSlash(f['url'])): f['type'].lower() for f in result} files = [] for onefile in filelist: fileInfo = {} fileFullPath = UtilFunc.toLinuxSlash(os.path.join(folder, onefile)) if UtilFunc.isHiddenFile(fileFullPath): continue if os.path.isfile(fileFullPath) and fileType: if not UtilFunc.matchFilter(onefile, filters[fileType]): continue fileInfo['folder'] = UtilFunc.toLinuxSlash(fileFullPath) fileInfo['type'] = 'all' fileInfo['isAdd'] = '0' if os.path.isdir(fileFullPath): fileInfo['isFolder'] = '1' if fileFullPath in selFolders.keys(): fileInfo['isAdd'] = '1' if UtilFunc.isWindowsSystem(): fileInfo['type'] = selFolders[unicode(fileFullPath)] else: fileInfo['type'] = selFolders[fileFullPath.encode( 'utf8')] else: fileInfo['isFolder'] = '0' par_path = os.path.dirname(fileFullPath) if par_path in selFolders.keys(): fileInfo['isAdd'] = '1' label_name = ProfileFunc.get_label_name(fileFullPath) if label_name: fileInfo['name'] = os.path.basename(label_name) else: fileInfo['name'] = os.path.basename(fileFullPath) files.append(fileInfo) if orderBy: cmpInfo = UtilFunc.httpArgToCmpInfo(orderBy) files.sort(lambda x, y: UtilFunc.dictInfoCmp(x, y, cmpInfo)) if limit >= 0: files = files[offset:(limit + offset)] else: files = files[offset:] ret = {} ret['folders'] = files ret['offset'] = offset ret['limit'] = len(files) return ret
def back(self, *arg, **params): global file_dir if file_dir not in ProfileFunc.GetBoxDisks(False): file_dir = os.path.dirname(file_dir) return self.index(storages=file_dir)
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})