def put(self): args = self.put_parser.parse_args() project = Project.query.filter_by(id=args["project_id"], owner=request.user).first_or_404() if args["name"] is not None: project.name = unicode(args["name"]) if args["platform"] is not None: project.platform = args["platform"] if args["logo"] is not None: new_logo = File.save(args["logo"]) if imghdr.what(new_logo.save_path) is None: new_logo.delete() return {'code': '400', 'msg': '上传的图片格式不正确'} if new_logo.size >= 500000: new_logo.delete() return {'code': '400', 'msg': '上传的图片已超过500KB'} if project.logo: File(project.logo).delete() project.logo = new_logo.fid if args["is_auto_publish"] is not None: project.is_auto_publish = args["is_auto_publish"] try: db.session.commit() except IntegrityError: if args['logo'] is not None: File(project.logo).delete() return {"code": '400', 'msg': '存在同名项目,修改项目失败'} data = {"project": project.to_dict()} return {"code": "200", "msg": "项目修改成功", "data": data}
def post(self): args = self.post_parser.parse_args() logo = args["logo"] if logo: logo_file = File.save(logo) logo_id = logo_file.fid if imghdr.what(logo_file.save_path) is None: logo_file.delete() return {'code': '400', 'msg': '上传的图片格式不正确'} if logo_file.size >= 500000: logo_file.delete() return {'code': '400', 'msg': '上传的图片已超过500KB'} else: logo_id = None name = unicode(args['name']) project = Project(name, request.user.id, args["platform"], logo_id, args["is_auto_publish"]) try: db.session.add(project) db.session.commit() except IntegrityError: if logo_id: File(logo_id).delete() return {"code": '400', 'msg': '存在同名项目,新增项目失败'} data = {"project": project.to_dict()} return {"code": "200", "msg": "创建项目成功", "data": data}
def delete(self): args = self.delete_parser.parse_args() # 1、删除logo 2、删除package及其dependents记录 3、删除project本身 project = Project.query.filter_by(id=args["project_id"], owner=request.user).first_or_404() if project.logo: logo = File(project.logo) logo.delete() for pkg in project.pkgs: pkg.dependents.delete() db.session.delete(pkg) db.session.delete(project) db.session.commit() return {"code": "200", "msg": "删除项目成功"}
def post(self): args = self.post_parser.parse_args() _file = args["file"] _file = File.save(_file) data = { "file_id": _file.id, "url": _file.url, "filename": _file.filename } return {"code": "200", "msg": "上传文件成功", "data": data}
def get(self, uid): project = Project.query.filter_by(uid=uid).first_or_404() packages = project.pkgs.filter_by( public_status=Package.public_on).order_by( Package.build_code.desc()) latest_package = None for pkg in packages: dpt_pkgs = pkg.dependents all_exists = True for dpt in dpt_pkgs: dpt_exists = Package.query.filter_by( project_id=dpt.project_id, version_name=dpt.version_name, public_status=Package.public_on).scalar() if not dpt_exists: all_exists = False break if all_exists: latest_package = pkg break else: continue if latest_package: package_file = File(latest_package.fid) args = self.get_parser.parse_args() if args['range']: range_match = re.search('^bytes=(\d+)-(\d*)$', args['range']) range_match = range_match.groups() start_byte = range_match[0] if start_byte: start_byte = int(start_byte) end_byte = range_match[1] if end_byte: end_byte = int(end_byte) else: end_byte = package_file.size file_data = get_partial_file(package_file, start_byte, end_byte) resp = Response(file_data, 206, mimetype=mimetypes.guess_type( package_file.save_path)[0], direct_passthrough=True) content_range = 'bytes {start}-{end}/{size}'.format( start=start_byte, end=end_byte, size=package_file.size) resp.headers.add('Content-Range', content_range) return resp else: counter = DayCounter.get_counter(project.uid) counter.increase() return send_file(package_file.save_path, as_attachment=True, attachment_filename=package_file.filename) else: abort(404)
def get(self, file_id): req_file = File(file_id) try: return send_file(req_file.save_path, as_attachment=True, attachment_filename=req_file.filename) except IOError: abort(404) except Exception as e: print str(e) abort(400)
def put(self): args = self.put_parser.parse_args() package = File.save(args["package"]) pkg_info = pkg_parser.start_parse(package) data = { "package_id": args["package_id"], "fid": package.fid, "filename": package.filename, "info": pkg_info } return {"code": "200", "msg": "解析安装包文件成功", "data": data}
def delete(self): args = self.delete_parser.parse_args() package_id = args["package_id"] package = Package.query.filter_by(id=package_id).join( Project, Package.project_id == Project.id).filter( Project.owner == request.user).first_or_404() # dpt_pkgs = package.dependents.delete() # db.session.delete(dpt_pkgs) package.dependents.delete() File(package.fid).delete() db.session.delete(package) db.session.commit() return {"code": "200", "msg": "删除安装包成功"}
def to_dict(self): data = {} data['id'] = self.id data['version_name'] = self.version_name data['build_code'] = int(self.build_code) data['fid'] = self.fid data['public_status'] = self.public_status data['public_status_display'] = self.PUBLIC_STATUS.get(self.public_status) data['update_level'] = self.update_level data['update_level_display'] = self.UPDATE_LEVEL.get(self.update_level) data['update_content'] = self.update_content data["download_url"] = settings.DOMAIN + url_for('package_download', fid=self.fid) pkg_file = File(self.fid) data['filename'] = pkg_file.filename data['filesize'] = pkg_file.size data['dependents'] = [dpt.to_dict() for dpt in self.dependents.all()] data['create_time'] = self.create_time.strftime('%Y-%m-%d %H:%M') return data
def get(self, fid): package = Package.query.filter_by(fid=fid).first_or_404() project = package.project counter = DayCounter.get_counter(project.uid) counter.increase() req_file = File(fid) args = self.get_parser.parse_args() if args['range']: range_match = re.search('^bytes=(\d+)-(\d*)$', args['range']) range_match = range_match.groups() start_byte = range_match[0] if start_byte: start_byte = int(start_byte) end_byte = range_match[1] if end_byte: end_byte = int(end_byte) else: end_byte = req_file.size file_data = get_partial_file(req_file, start_byte, end_byte) resp = Response(file_data, 206, mimetype=mimetypes.guess_type( req_file.save_path)[0], direct_passthrough=True) content_range = 'bytes {start}-{end}/{size}'.format( start=start_byte, end=end_byte, size=req_file.size) resp.headers.add('Content-Range', content_range) return resp else: try: return send_file(req_file.save_path, as_attachment=True, attachment_filename=req_file.filename) except IOError: abort(404) except Exception as e: print str(e) abort(400)
def put(self): args = self.put_parser.parse_args() package_id = args["package_id"] package = Package.query.filter_by(id=package_id).join( Project, Package.project_id == Project.id).filter( Project.owner == request.user).first_or_404() if args["version_name"]: package.version_name = args["version_name"] if args["build_code"]: package.build_code = args["build_code"] if args["fid"] and args["fid"] != package.fid: File(package.fid).delete() package.fid = args["fid"] if args["update_level"]: package.update_level = args["update_level"] if args["public_status"]: # 这段代码你可能看不懂,但是就应该这么写 package.public_status = args["public_status"] else: package.update_content = args["update_content"] if args["dependent_pkgs"]: try: # dpt_pkgs = package.dependents # db.session.delete(dpt_pkgs) package.dependents.delete() dependent_pkgs = json.loads(args["dependent_pkgs"]) for pkg in dependent_pkgs: dependent_pkg = PackageDependent(package.id, pkg["project_id"], pkg["version_name"]) db.session.add(dependent_pkg) db.session.commit() except Exception as e: print e db.session.commit() data = {"package": package.to_dict()} return {"code": "200", "msg": "修改安装包信息成功", "data": data}
def file(self): return File(self.fid)