def cron(self, crontab=None, user=None): file = None if crontab: crontab = secure_filename(crontab) file = '/'.join(['/etc/cron.d', crontab]) if user: user = secure_filename(user) file = '/'.join(['/var/spool/cron/crontabs', user]) if file: try: with open(file, 'r') as f: read_data = '<html><body><pre>' + f.read() + \ '</pre></body></html>' output = read_data output.replace(' ', ' ').replace('\n', '<br>') except (OSError, IOError): output = '<html><body>File not found: %s </body></html>' % file return output else: return self.usage
def post_upload(): file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) savename = get_save_name(filename) filepath = os.path.join(app.config.get('UPLOAD_DIR'), savename) file.save(filepath) staticfilepath = filepath[1:].replace("\\", "/") bobj = {"filename": filename, "url": staticfilepath, "error": False} if os.path.exists( os.path.join(app.config.get('CONTENT_DIR'), "uploads.json")): fd = open( os.path.join(app.config.get('CONTENT_DIR'), "uploads.json"), "r") _s = fd.read() fd.close() _os = json.loads(_s) else: _os = {} _os[savename] = filename _s = json.dumps(_os) fd = open(os.path.join(app.config.get('CONTENT_DIR'), "uploads.json"), "w") fd.write(_s) fd.close() else: bobj = {'error': True} if not bobj['error']: save_uploadfile_to_backup(filepath) return json.dumps(bobj)
def post(self): response = {} admin_id = yield self.admin_author() if not admin_id: self.set_status(401) response['msg'] = "Auth deny" self.write(response) return category = str(self.get_body_argument("category", '')) title = str(self.get_body_argument("title", '')) description = str(self.get_body_argument("description", '')) value = int(self.get_body_argument("value", 0)) flag = str(self.get_body_argument("flag", '')) if not category or not title or not description or not value or not flag: self.set_status(400) response['msg'] = "Malformed Request" self.write(response) return challenge_id = shortid_generate() challenge = {'id': challenge_id, 'category': category, 'title': title, 'description': description, 'value': value, 'flag': flag, 'files': False, 'hidden': False} files = self.request.files if files: challenge['files'] = True for filelist in files: for afile in files[filelist]: filename = secure_filename(afile.get('filename', '')) if not len(filename): continue md5hash = hashlib.md5(os.urandom(64)).hexdigest() if not os.path.exists(os.path.join(os.path.normpath(self.settings.get("static_path")), 'uploads', md5hash)): os.makedirs(os.path.join(os.path.normpath(self.settings.get("static_path")), 'uploads', md5hash)) location = os.path.join(os.path.normpath(self.settings.get("static_path")).split('/')[-1], 'uploads', md5hash, filename) with open(location, 'w') as f: f.write(afile.get('body', '')) file_res = yield self.db.files.insert({'chalid': challenge_id, 'location': location}) if not file_res: self.set_status(404) response['msg'] = "File Create Error." self.write(response) return try: result = yield self.db.challenges.insert(challenge) except Exception as e: # add log here self.set_status(404) response['msg'] = "Challenge Create Error." self.write(response) return if result: self.set_status(201) response['msg'] = "Challenge Create Success" self.write(response) return
def fetch(self, url, filename): """Pass through fetcher.""" file = urllib2.urlopen(url) file_data = file.read() with open(filename, 'wb') as f: f.write(file_data) if not os.path.exists(filename): raise RuntimeError('Downloaded file is not at this path ' '{}'.format(filename)) return secure_filename(filename)
def upload_file(): if request.method == 'POST': # 如果是 POST 请求方式 file = request.files['file'] # 获取上传的文件 if file: # if file and allowed_file(file.filename): # 如果文件存在并且符合要求则为 true filename = secure_filename(file.filename) # 获取上传文件的文件名 if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) file.save(os.path.join(UPLOAD_FOLDER, filename)) # 保存文件 # return '{} upload successed!'.format(filename) # 返回保存成功的信息 return redirect(url_for("upload_file")) # 重定向到上传页面 # 使用 GET 方式请求页面时或是上传文件失败时返回上传文件的表单页面 return '''
def dead(): """ Video of dead cow :return: """ # get the params first user_id = g.user.userid entity = request.form.get('entity') utils.verify_param(abort, logger, error_code=400, entity=entity, method_name="dead") json_obj = json.loads(entity) company_id = json_obj.get('companyid') gather_time1 = json_obj.get('gathertime') gather_time = utils.verify_time_param(abort, logger, gather_time1) rfid_code = json_obj.get('rfidcode') ip = json_obj.get('ip') imei = json_obj.get('imei') try: video = request.files['video'] except: video = None # verify the existence of parameters utils.verify_param(abort, logger, error_code=400, user_id=user_id, json_obj=json_obj, company_id=company_id, gather_time=gather_time, rfid_code=rfid_code, ip=ip, imei=imei, video=video, method_name="dead") # Judging video size video_size = len(video.read()) / float(1000.0) if video_size > config.max_video_size: logger.error( 'From ' + ip + ' -> Upload dead cow video file : ' + video.filename + ' with size of {} kb , But video_size over 20MB failed to upload'.format( video_size)) abort(413) # make the save folder path and save the dead cow video folder_path = os.path.join(config.base_images_path, "dead_{}_{}".format(company_id, rfid_code)) + os.sep if not os.path.exists(folder_path): os.makedirs(folder_path) video.seek(0) video.save(folder_path + utils.secure_filename(video.filename)) logger.info('From ' + ip + ' -> Upload dead cow video file : ' + video.filename + ' with size of {} kb'.format( video_size)) # Change the health of cow try: cow = Archives.query.filter_by(rfid_code=rfid_code, company_id=company_id).first() cow.health_status = "0" db.session.commit() logger.info( "cow rfid_code = {} from company_id = {} successful to modify health_status".format(rfid_code, company_id)) except: db.session.rollback() shutil.rmtree(folder_path) logger.error( "cow rfid_code = {} from company_id = {} failed to modify health_status , so delete the folder".format( rfid_code, company_id)) abort(502) return jsonify({ 'userid': user_id, 'companyid': company_id, 'rfidcode': rfid_code, 'gathertime': str(gather_time), 'ip': ip, 'imei': imei, })
def verify(): """ verify params and save the cow information to the db :return: cow information """ # get the params first user_id = g.user.userid entity = request.form.get('entity') utils.verify_param(abort, logger, error_code=400, entity=entity, method_name="verify") json_obj = json.loads(entity) company_id = json_obj.get('companyid') gather_time1 = json_obj.get('gathertime') gather_time = utils.verify_time_param(abort, logger, gather_time1) rfid_code = json_obj.get('rfidcode') ip = json_obj.get('ip') imei = json_obj.get('imei') xvalue = json_obj.get('xvalue') yvalue = json_obj.get('yvalue') width = json_obj.get('width') height = json_obj.get('height') try: video = request.files['video'] except: video = None # give the age value, 0 for default now. age = 0 # json_obj.get("age") # give the health_status value, 1 for default now. health_status = '1' # json_obj.get("health_status") # verify the existence of parameters utils.verify_param(abort, logger, error_code=400, user_id=user_id, json_obj=json_obj, company_id=company_id, gather_time=gather_time, rfid_code=rfid_code, ip=ip, imei=imei, xvalue=xvalue, yvalue=yvalue, width=width, height=height, video=video, age=age, health_status=health_status, method_name="verify") # judge the existence of cow if Archives.query.filter_by(rfid_code=rfid_code, company_id=company_id).first(): logger.error("cow rfid_code = {} already exists and cannot be save repeatedly".format(rfid_code)) abort(403) else: # Judging video size video_size = len(video.read()) / float(1000.0) if video_size > config.max_video_size: logger.error( 'From ' + ip + ' -> Upload video file : ' + video.filename + ' with size of {} kb , But video_size over 20MB failed to upload'.format( video_size)) abort(413) # make the save folder path and save the video folder_path = os.path.join(config.base_images_path, company_id, rfid_code) + os.sep if not os.path.exists(folder_path): os.makedirs(folder_path) video.seek(0) video.save(folder_path + utils.secure_filename(video.filename)) logger.info('From ' + ip + ' -> Upload video file : ' + video.filename + ' with size of {} kb'.format( video_size)) # make async execution thread for the video save and frame grabber executor.submit(utils.process_video_to_image, abort, logger, video, folder_path, rfid_code, xvalue, yvalue, width, height) # assign values to database fields archives = Archives(rfid_code=rfid_code, age=age, company_id=company_id, gather_time=gather_time, health_status=health_status, folder_path=os.path.join(config.base_images_path, company_id, rfid_code), extra_info='file name is : ' + video.filename) li = LogInfo(company_id=company_id, rfid_code=rfid_code, remote_ip=ip, imei=imei, extra_info='file name is : ' + video.filename) db_list = [archives, li] # logs the submit info to the db utils.insert_record(logger, db_list, db, abort, company_id, rfid_code, folder_path) logger.info( "cow rfid_code = {} from company_id = {} was successfully inserted into the database".format(rfid_code, company_id)) return jsonify({ 'userid': user_id, 'companyid': company_id, 'resoult': True, 'gathertime': str(gather_time), 'verinfo': 'jobs was launched in background', 'ip': ip, 'imei': imei, })
def verify(): """ verify params and save the cow information to the db :return: cow information """ # get the params first user_id = g.user.userid json_obj = json.loads(request.form.get('entity')) company_id = json_obj.get('companyid') gather_time1 = json_obj.get('gathertime') try: gather_time = datetime.datetime.strptime(gather_time1, "%Y/%m/%d %H:%M:%S") except: gather_time = gather_time1 rfid_code = json_obj.get('rfidcode') ip = json_obj.get('ip') imei = json_obj.get('imei') try: video = request.files['video'] except: video = None # give the age value, 0 for default now. age = 0 # json_obj.get("age") # give the health_status value, 1 for default now. health_status = '1' # json_obj.get("health_status") # verify the existence of parameters utils.verify_param(abort, error_code=400, user_id=user_id, json_obj=json_obj, company_id=company_id, gather_time=gather_time, rfid_code=rfid_code, ip=ip, imei=imei, video=video, age=age, health_status=health_status) # judge the existence of cow if Archives.query.filter_by(rfid_code=rfid_code).first(): abort(403) else: # make the save folder path and save the video folder_path = os.path.join(config.base_images_path, company_id, rfid_code) + os.sep if not os.path.exists(folder_path): os.makedirs(folder_path) video.save(folder_path + utils.secure_filename(video.filename)) # make async execution thread for the video save and frame grabber executor.submit( utils.process_video_to_image, video, os.path.join(config.base_images_path, company_id, rfid_code) + os.sep, rfid_code) # assign values to database fields archives = Archives(rfid_code=rfid_code, age=age, company_id=company_id, gather_time=gather_time, health_status=health_status, folder_path=os.path.join(config.base_images_path, company_id, rfid_code), extra_info='file name is : ' + video.filename) li = LogInfo(company_id=company_id, rfid_code=rfid_code, remote_ip=ip, imei=imei, extra_info='file name is : ' + video.filename) db_list = [archives, li] # log the submit info to the db utils.insert_record(db_list, db, abort) return jsonify({ 'userid': user_id, 'companyid': company_id, 'resoult': True, 'gathertime': str(gather_time), 'verinfo': 'jobs was launched in background', 'ip': ip, 'imei': imei, })
def put(self): response = {} admin_name = yield self.admin_author() if not admin_name: self.set_status(401) response['msg'] = "Auth deny" self.write(response) return category = str(self.get_body_argument("category", '')) title = str(self.get_body_argument("title", '')) description = str(self.get_body_argument("description", '')) value = int(self.get_body_argument("value", 0)) flag = str(self.get_body_argument("flag", '')) if not category or not title or not description or not value or not flag: self.set_status(400) response['msg'] = "Malformed Request" self.write(response) return challenge_ID = self.request.uri.split("/")[3] challenge = yield self.db.challenges.find_one({'id': challenge_ID}) if not challenge: self.set_status(400) response['msg'] = "Malformed Request" self.write(response) return docu_update = {'category': category, 'title': title, 'description': description, 'value': value, 'flag': flag,} files = self.request.files if files: remove_files_res = yield self.db.files.remove({'chalid': challenge_ID}) #don't remove files, This only remove the index of files in database for filelist in files: for afile in files[filelist]: filename = secure_filename(afile.get('filename', '')) if not len(filename): continue md5hash = hashlib.md5(os.urandom(64)).hexdigest() if not os.path.exists(os.path.join(os.path.normpath(self.settings.get("static_path")), 'uploads', md5hash)): os.makedirs(os.path.join(os.path.normpath(self.settings.get("static_path")), 'uploads', md5hash)) location = os.path.join(os.path.normpath(self.settings.get("static_path")).split('/')[-1], 'uploads', md5hash, filename) with open(location, 'w') as f: f.write(afile.get('body', '')) file_res = yield self.db.files.insert({'chalid': challenge_id, 'location': location}) if not files_res: self.set_status(404) response['msg'] = "File Create Error." self.write(response) return c_res_upda = yield self.db.challenges.update({'id': str(challenge.get('id', ''))}, {'$set': docu_update}) if not c_res_upda['ok']: self.set_status(404) response['msg'] = "Change " + challenge_ID + " Error" self.write(response) return diff_value = challenge.get('value', 0) - value if diff_value: solved_users = yield self.db.solves.find({'chalid': challenge_ID}).to_list(None) for solved_user in solved_users: user = yield self.db.users.find_one({'id': str(solved_user.get('userid', ''))}) user_score = int(user.get('score', 0)) + diff_value u_res_upda = yield self.db.users.update({'id': str(user.get('id', ''))}, {'$set': {'score': user_score}}) if not u_res_upda['ok']: self.set_status(404) response['msg'] = "Change user score Error" self.write(response) return response['msg'] = "Change " + challenge_ID + " Success" self.write(response) return
def fetch(self, disk_path): """Pass through fetcher""" if not os.path.exists(disk_path): raise RuntimeError( 'Cannot find a file at path {}'.format(disk_path)) return secure_filename(disk_path)