public DtoActionResult Delete(int id) { var result = new ServiceAttachment().Delete(id); if (result == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } return(result); }
public async stt::Task GetRequestObjectAsync() { moq::Mock <ServiceAttachments.ServiceAttachmentsClient> mockGrpcClient = new moq::Mock <ServiceAttachments.ServiceAttachmentsClient>(moq::MockBehavior.Strict); mockGrpcClient.Setup(x => x.CreateOperationsClientForRegionOperations()).Returns(new moq::Mock <lro::Operations.OperationsClient>().Object); GetServiceAttachmentRequest request = new GetServiceAttachmentRequest { Region = "regionedb20d96", Project = "projectaa6ff846", ServiceAttachment = "service_attachment592c837a", }; ServiceAttachment expectedResponse = new ServiceAttachment { Id = 11672635353343658936UL, TargetService = "target_service3f6f9a5a", Kind = "kindf7aa39d9", Name = "name1c9368b0", CreationTimestamp = "creation_timestamp235e59a1", ConnectedEndpoints = { new ServiceAttachmentConnectedEndpoint(), }, Region = "regionedb20d96", ConsumerRejectLists = { "consumer_reject_lists640993ba", }, Fingerprint = "fingerprint009e6052", ProducerForwardingRule = "producer_forwarding_rule8732a25d", ConnectionPreference = "connection_preference328ae231", EnableProxyProtocol = true, NatSubnets = { "nat_subnets59063249", }, ConsumerAcceptLists = { new ServiceAttachmentConsumerProjectLimit(), }, Description = "description2cf9da67", SelfLink = "self_link7e87f12d", PscServiceAttachmentId = new Uint128(), }; mockGrpcClient.Setup(x => x.GetAsync(request, moq::It.IsAny <grpccore::CallOptions>())).Returns(new grpccore::AsyncUnaryCall <ServiceAttachment>(stt::Task.FromResult(expectedResponse), null, null, null, null)); ServiceAttachmentsClient client = new ServiceAttachmentsClientImpl(mockGrpcClient.Object, null); ServiceAttachment responseCallSettings = await client.GetAsync(request, gaxgrpc::CallSettings.FromCancellationToken(st::CancellationToken.None)); xunit::Assert.Same(expectedResponse, responseCallSettings); ServiceAttachment responseCancellationToken = await client.GetAsync(request, st::CancellationToken.None); xunit::Assert.Same(expectedResponse, responseCancellationToken); mockGrpcClient.VerifyAll(); }
public HttpResponseMessage GetAttachment(int id) { var attachment = new ServiceAttachment().Get(id); if (attachment == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } var basePath = ServiceSetting.GetSettingValue(SettingStrings.StoragePath); using (var unc = new UncServices()) { if (unc.NetUseWithCredentials() || unc.LastError == 1219) { var fullPath = Path.Combine(basePath, "attachments", attachment.DirectoryGuid, attachment.Name); if (File.Exists(fullPath)) { HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); try { var stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); result.Content.Headers.ContentDisposition.FileName = attachment.Name; return(result); } catch (Exception ex) { Logger.Error(ex.Message); } } } else { throw new HttpException("Could Not Reach Storage Path"); } } return(new HttpResponseMessage(HttpStatusCode.NotFound)); }
public void AttachmentChunkingComplete() { var fileName = Request["qqfilename"]; if (string.IsNullOrEmpty(fileName) || fileName == Path.DirectorySeparatorChar.ToString()) { throw new HttpException(); } var attachmentGuid = Request["attachmentGuid"]; if (string.IsNullOrEmpty(attachmentGuid)) { throw new HttpException(); } var computerId = Request["computerId"]; var assetId = Request["assetId"]; if (assetId == null && computerId == null) { throw new HttpException(); } var userId = Convert.ToInt32(((ClaimsIdentity)User.Identity).Claims.Where(c => c.Type == "user_id") .Select(c => c.Value).SingleOrDefault()); var userName = new ServiceUser().GetUserName(userId); var attachment = new EntityAttachment(); attachment.AttachmentTime = DateTime.Now; attachment.DirectoryGuid = attachmentGuid; attachment.Name = fileName; attachment.UserName = userName; var result = new ServiceAttachment().Add(attachment); if (!result.Success) { throw new HttpException(); } if (assetId != null) { var asset = new EntityAssetAttachment(); asset.AssetId = Convert.ToInt32(assetId); asset.AttachmentId = attachment.Id; result = new ServiceAssetAttachment().Add(asset); if (!result.Success) { throw new HttpException(); } } if (computerId != null) { var computer = new EntityComputerAttachment(); computer.ComputerId = Convert.ToInt32(computerId); computer.AttachmentId = attachment.Id; result = new ServiceComputerAttachment().Add(computer); if (!result.Success) { throw new HttpException(); } } }
private string SaveAs(string type) { var filePath = Path.Combine(_upload.DestinationDirectory, _upload.Filename); using (var unc = new UncServices()) { if (unc.NetUseWithCredentials() || unc.LastError == 1219) { try { using (var file = new FileStream(filePath, FileMode.Create)) _upload.InputStream.CopyTo(file); } catch (Exception ex) { return(ex.Message); } if (type.Equals("module")) { var uploadedFile = new EntityUploadedFile(); uploadedFile.Name = _upload.Filename; uploadedFile.Guid = _upload.ModuleGuid; uploadedFile.Hash = Utility.GetFileHash(filePath); var result = new ServiceUploadedFile().AddFile(uploadedFile); if (!result.Success) { try { File.Delete(filePath); } catch { //ignored } return("Could Not Update Database"); } } else if (type.Equals("attachment")) { var attachment = new EntityAttachment(); attachment.AttachmentTime = DateTime.Now; attachment.DirectoryGuid = _upload.AttachmentGuid; attachment.Name = _upload.Filename; attachment.UserName = _upload.Username; var result = new ServiceAttachment().Add(attachment); if (!result.Success) { throw new HttpException(); } if (_upload.AssetId != null) { var asset = new EntityAssetAttachment(); asset.AssetId = Convert.ToInt32(_upload.AssetId); asset.AttachmentId = attachment.Id; result = new ServiceAssetAttachment().Add(asset); if (!result.Success) { throw new HttpException(); } } if (_upload.ComputerId != null) { var computer = new EntityComputerAttachment(); computer.ComputerId = Convert.ToInt32(_upload.ComputerId); computer.AttachmentId = attachment.Id; result = new ServiceComputerAttachment().Add(computer); if (!result.Success) { throw new HttpException(); } } } } else { return("Could Not Reach Storage Path"); } } return(null); }