def delete_document(document_id,user): document = ProjectDocument.objects.get(int(document_id)) document_name = '--' file_info = FileInfo.objects.get(document.FileID) if file_info: document_name = file_info.FileName if document.Type == 1: child_documents = ProjectDocument.objects.get_child_documents(document.id) if len(child_documents)>0: DocumentService.delete_child_document(child_documents) document.delete() else: file_info = FileInfo.objects.get(document.FileID) file_info.delete() document.delete() if document.Type == 2: file_info = FileInfo.objects.get(document.FileID) FileInfoService.delete_value(file_info.id, ProjectExcelDocumentMongoFile) document.delete() if document.Type == 3: file_info = FileInfo.objects.get(document.FileID) FileInfoService.delete_value(file_info.id, ProjectDocumentMongoFile) document.delete() DocumentService.log_delete_activity(user,document,document_name) pass
def delete_attachment(file_id, issue_id): FileInfoService.delete_file(int(file_id), mongo_model=IssueMongoFile) if int(issue_id) != 0: issue = ProjectIssue.objects.get(int(issue_id)) issue.Attachments = issue.Attachments.replace( str(file_id) + ",", "") issue.save()
def copy_package(history): ''' copy release package to release resp ''' result = list() if history.PackageID and not CITaskHistoryService.is_history_archived( history): history_files = list() new_archive = CITaskHistoryService.new_archive( history, history_files) for old_file_id in eval(history.PackageID): file_id = 0 try: file_id = FileInfoService.copy_file( old_file_id, PackgeMongoFile, ReleaseArchiveMongoFile) result.append(file_id) history_files.append(file_id) except Exception as ex: FileInfoService.delete_file(file_id, ReleaseArchiveMongoFile) new_archive.delete() SimpleLogger.exception(ex) result = None if result: file_ids = "" for file_id in history_files: file_ids = file_ids + str(file_id) + "," new_archive.Archives = file_ids new_archive.save() return result
def delete_file(request, package_file_id): result = True try: FileInfoService.delete_file(int(package_file_id), IssueMongoFile) except Exception as ex: result = str(ex) SimpleLogger.exception(ex) return HttpResponse(result)
def update_avatar(user, cached_file_keys): file_list = AccountService.store_cached_file(cached_file_keys) if len(file_list) > 0: if user.extend_info.avatar.isnumeric(): FileInfoService.delete_file(int(user.extend_info.avatar), mongo_model=UCenterMongoFile) user.extend_info.avatar = file_list[0] user.extend_info.save() else: user.extend_info.avatar = file_list[0] user.extend_info.save() return AccountService.get_avatar_url(user, )
def get(self, request, *args, **kwargs): result = True try: file_id = kwargs.get('file_id') temp_file = FileInfoService.get_file(int(file_id), TempFileMongoFile) if temp_file: result = temp_file else: result = FileInfoService.get_file(int(file_id), IssueMongoFile) except Exception as ex: result = str(ex) SimpleLogger.exception(ex) return HttpResponse(result, content_type="application/octet-stream")
def get(self, request, *args, **kwargs): result = "ok" try: file_id = self.kwargs['file_id'] if file_id == "all": all_invalid_files = FileInfo.objects.all(is_active=0).filter( IsActive=0) for file in all_invalid_files: FileInfoService.clean_build_archive(file.id) else: FileInfoService.clean_build_archive(file_id) except Exception as ex: result = str(ex) SimpleLogger.exception(ex) return Response(str(result))
def put(self, request, *args, **kwargs): user_agent = request.META['HTTP_USER_AGENT'] output_id = int(self.kwargs.get("output_id", 0)) output = CITaskStepOutput.objects.get(output_id) if output: if 'MAC' in user_agent.upper(): FileInfoService.create_package_plist(output.id) # FileInfoService.create_package_file(output.id) package = WEB_HOST.replace( 'http', 'https') + "/static/plist_files/" + str( output.id) + ".plist" else: package = WEB_HOST + "/api/ci/task/output/" + str( output_id) + "/download" return Response({"package_url": package})
def delete_output(stage_historyid): try: outputs = CITaskStepOutput.objects.get_stage_output(int(stage_historyid)) for output in outputs: if output.ProductID: if output.ProductType in (2,3,6): FileInfoService.delete_file(output.ProductID) CITaskHistoryService.clean_temp_file(output.ProductID) if output.ProductType == 1: FileInfoService.delete_file(output.ProductID,mongo_model=BuildLogMongoFile) CITaskHistoryService.clean_temp_file(output.ProductID) if output.ProductType == 5: PackgeMongoFile.objects.delete_value(output.ProductID) output.delete() except Exception as ex: SimpleLogger.exception(ex)
def store_cached_file(cached_file_keys, project_id, parent, user): result = list() keys = cached_file_keys for key in keys: if key != "": temp_file = TempFileMongoFile.objects.get(key) if temp_file != None: mongo_id = ProjectDocumentMongoFile.objects.copy_bucket( temp_file) valid_data = dict() if str(parent) == "": valid_data['Parent'] = None else: valid_data['Parent'] = parent valid_data['ProjectID'] = project_id valid_data['Type'] = 3 valid_data['FileName'] = temp_file.metadata[ 'file_real_name'] print(1) file_id = FileInfoService.add_file( 0, mongo_id, temp_file.metadata['file_real_name'], 1, 0, temp_file.length) print(file_id) document = DocumentService.create_document( valid_data, user, False) document.FileID = file_id document.save() TempFileMongoFile.objects.delete_file(key) result.append(document.id) return result
def download_package(request,file_id): try: file=FileInfo.objects.get(int(file_id)) contents=FileInfoService.download_file(file_id) def file_iterator(chunk_size=1024*50): while True: c = contents.read(chunk_size) if c: yield c else: break result=StreamingHttpResponse(file_iterator(), content_type='application/octet-stream') display_file_name=str(file.FileName.encode("utf-8")).replace("'","") result['Content-Disposition'] = 'attachment;filename="'+display_file_name+'"' except Exception as ex: result=HttpResponse(str(ex)) SimpleLogger.exception(ex) return result
def attachments_upload_handler(file): message = "" mongo_file_id = UploadFileService.save_to_mongo(file) file_id = FileInfoService.add_file(0, mongo_file_id, file.name, 1, 0, file.size) if file_id != 0: message = str(file_id) return message
def download_document(file_id): if file_id.isnumeric(): document = ProjectDocument.objects.get(int(file_id)) temp_file = FileInfoService.get_file(int(document.FileID), ProjectDocumentMongoFile) else: temp_file = TempFileMongoFile.objects.get(file_id) return temp_file
def mobile_download_page(request): user_agent=request.META['HTTP_USER_AGENT'] host=request.META['HTTP_HOST'] file_id=request.GET.get('file_id',0) history_id=request.GET.get('history_id',0) os_name="Android" android=True if 'MAC' in user_agent.upper(): FileInfoService.create_package_plist(file_id,history_id) FileInfoService.create_package_file(file_id) package=WEB_HOST.replcae("http","https")+"/static/plist_files/"+str(file_id)+".plist" os_name="iPhone" android=False else: package=WEB_HOST+"/ci/history/"+str(file_id)+"/download_package" page_fileds={'package':package,"os_name":os_name,'android':android} return render_to_response('task_history/ci_mobile_package_download.html',page_fileds)
def attachments_upload_handler(file): message = [] mongo_file_id = ForTestingService.save_to_mongo(file) file_id = FileInfoService.add_file(0, mongo_file_id, file.name, 1, 0, file.size) if file_id != 0: message.append(file_id) message.append(str(mongo_file_id)) return message
def download_file(request, package_file_id): result = True try: file_info = FileInfo.objects.get(int(package_file_id)) result = FileInfoService.get_file(int(package_file_id), IssueMongoFile) except Exception as ex: result = str(ex) SimpleLogger.exception(ex) return HttpResponse(result, content_type="application/" + file_info.FileSuffixes)
def delete_child_document(child_documents): for document in child_documents: if document.Type == 1: childs = ProjectDocument.objects.get_child_documents(document.id) if len(childs)>0: DocumentService.delete_child_document(childs) document.delete() else: file_info = FileInfo.objects.get(document.FileID) file_info.delete() document.delete() if document.Type == 2: file_info = FileInfo.objects.get(document.FileID) FileInfoService.delete_value(file_info.id,ProjectExcelDocumentMongoFile) document.delete() if document.Type == 3: file_info = FileInfo.objects.get(document.FileID) FileInfoService.delete_value(file_info.id,ProjectDocumentMongoFile) document.delete() return 0
def attachments_upload_handler(file): message = [] mongo_message = IssueService.save_to_mongo(file, IssueMongoFile) if mongo_message[0] != "0": file_id = FileInfoService.add_file(0, mongo_message[0], file.name, 1, 0, file.size) message.append(file_id) message.append(mongo_message[0]) else: message[0] = "0" message[1] = "长传文件失败," + mongo_message[1] return message
def get_issue_attachment_viewer(self,issue_id,attachment_id): temp_file=FileInfoService.get_file(int(attachment_id),IssueMongoFile).read() kind = filetype.guess(temp_file) file_type=0 file_content="" if kind: if kind.mime.startswith("image"): file_type=1 if kind.mime.startswith("video"): file_type=2 pagefileds={'file_id':attachment_id,'file_type':file_type,'issue_id':issue_id} return self.get_webpart(pagefileds,ProjectIssuePath.issue_attachment_viewer)
def upload_package(request): result=0 tq_id=request.POST.get('tq_id','') file_type=request.POST.get('file_type','') upload_file=request.FILES['upload_file'] max_file_size=SystemConfigService.get_upload_file_maxsize() file_wihte_list=SystemConfigService.get_file_type_white_list() if CITaskService.validate_upload_file(upload_file,max_file_size,file_wihte_list): mongo_fileid=MongoDBService.save_file(upload_file,PackgeMongoFile) result=FileInfoService.add_file(0,mongo_fileid, upload_file.name,1,0,upload_file.size) CITaskService.update_ci_taskhistory(tq_id,result,file_type) return result
def history_clean_worker(task_historys): for history in task_historys: try: if history.PackageID: for fileid in eval(history.PackageID): FileInfoService.delete_file(fileid) CITaskHistoryService.clean_temp_file(fileid) if history.LogFileID: for fileid in eval(history.LogFileID): FileInfoService.delete_file(fileid) CITaskHistoryService.clean_temp_file(fileid) if history.BuildLogID: FileInfoService.delete_file(history.BuildLogID, mongo_model=BuildLogMongoFile) history.BuildLogID = 0 if history.ChangeLog: PackgeMongoFile.objects.delete_value(history.ChangeLog) history.IsActive = 0 except Exception as ex: SimpleLogger.exception(ex) history.IsActive = 1 continue finally: history.save()
def get(self, request, *args, **kwargs): result = True try: file_id = kwargs.get('file_id') file_info = FileInfo.objects.get(int(file_id)) result = FileInfoService.get_file(int(file_id), FortestingMongoFile) except Exception as ex: result = str(ex) SimpleLogger.exception(ex) return HttpResponse(result, content_type="application/" + file_info.FileSuffixes)
def save_build_log(tq_id): file_name = "ci_build_log" + tq_id + ".log" build_log = RedisService.get_value("ci_build_log" + tq_id) result = MongoDBService.save_content_asfile(file_name, build_log, BuildLogMongoFile) file_id = FileInfoService.add_file(0, result, file_name, 1, 0, len(build_log)) task_history = CITaskHistory.objects.get_by_tqid(int(tq_id)) if file_id: task_history.BuildLogID = file_id else: task_history.BuildLogID = 0 task_history.save()
def create_document(validate_data,user): document = ProjectDocument() document.Owner = user.id document.Parent = validate_data.get("Parent",None) document.ProjectID = validate_data.get("ProjectID",0) document.ReadOnly = False document.Type = validate_data.get("Type",0) document_name = validate_data.get("FileName",'新建文件') file_id = FileInfoService.add_file(document.Parent,'',document_name,document.Type,user.id,0) document.FileID = file_id document.save() DocumentService.log_create_activity(user,document,document_name) return document
def upload_package(request): result = None upload_file = request.FILES['upload_file'] max_file_size = SystemConfigService.get_upload_file_maxsize() file_wihte_list = SystemConfigService.get_file_type_white_list() if CITaskHistoryService.validate_upload_file(upload_file, max_file_size, file_wihte_list): mongo_fileid = MongoDBService.save_file(upload_file, PackgeMongoFile) file_id = FileInfoService.add_file(0, mongo_fileid, upload_file.name, 1, 0, upload_file.size) temp_output = CITaskStepOutput() temp_output = CITaskHistoryService.init_step_output(temp_output,request.data) temp_output.ProductID = file_id temp_output.save() result = temp_output return result
def store_cached_file(cached_file_keys): result = "" keys = cached_file_keys for key in keys: if key != "": temp_file = TempFileMongoFile.objects.get(key) print(temp_file.metadata) print(temp_file.content_type) print(temp_file.length) if temp_file != None: mongo_id = IssueMongoFile.objects.copy_bucket(temp_file) file_id = FileInfoService.add_file(0, mongo_id, temp_file.metadata['file_real_name'], 1, 0, temp_file.length) if file_id != 0: result = result + str(file_id) + "," return result
def store_cached_file(cached_file_keys): result = list() keys = cached_file_keys for key in keys: if key != "": temp_file = TempFileMongoFile.objects.get(key) if temp_file is not None: mongo_id = UCenterMongoFile.objects.copy_bucket(temp_file) file_id = FileInfoService.add_file( 0, mongo_id, temp_file.metadata['file_real_name'], 1, 0, temp_file.length) TempFileMongoFile.objects.delete_file(key) if file_id != 0: result.append(file_id) return result
def store_cached_file(cached_file_keys): result = "" keys = cached_file_keys.split(',') for key in keys: if key != "": temp_file = RedisService.get_object(key) if temp_file != None: RedisService.delete_value(key) mongo_id = MongoDBService.save_file( temp_file, IssueMongoFile) file_id = FileInfoService.add_file(0, mongo_id, temp_file.name, 1, 0, temp_file.size) if file_id != 0: result = result + str(file_id) + "," return result
def get_file_stream(self, file_id): file = FileInfo.objects.get(int(file_id)) contents = FileInfoService.download_file(file_id) def file_iterator(chunk_size=1024 * 50): while True: c = contents.read(chunk_size) if c: yield c else: break result = StreamingHttpResponse(file_iterator(), content_type='application/octet-stream') result[ 'Content-Disposition'] = 'attachment;filename="' + file.FileName + '"' return result
def save_build_log(tq_id,tq_done=False): file_name="ci_build_log"+tq_id+".log" build_log=RedisService.get_value("ci_build_log"+tq_id) result=MongoDBService.save_content_asfile(file_name, build_log,BuildLogMongoFile) file_id=FileInfoService.add_file(0,result,file_name,1,0,len(build_log)) RedisService.delete_value("ci_build_log" + tq_id) if tq_done: temp_output = CITaskStepOutput() temp_output.ProductType = 1 temp_output.ProductID = file_id task_queue = TaskQueue.objects.get(int(tq_id)) task_history = CITaskHistory.objects.get_history_by_uuid(task_queue.TaskUUID) temp_output.TaskHistoryID = task_history.id temp_output.TaskID = task_history.CITaskID temp_output.StepID = "" temp_output.StageHistoryID = 0 temp_output.save() else: RedisService.set_value("ci_build_log"+tq_id,"") return file_id