public HttpResponseMessage Get(string size, string id) { using (var picturesService = new PicturesService()) { FileStream fileStream = null; HttpResponseMessage result ; string rootPath = HttpContext.Current.Server.MapPath("~/data/pics"); PicSize picSize = (PicSize)Enum.Parse(typeof(PicSize), size); using (picturesService.GetImageStream(rootPath, picSize,id, out fileStream)) { if (fileStream.CanRead) { int length = (int)fileStream.Length; byte[] buffer = new byte[length]; fileStream.Read(buffer, 0, length); result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new ByteArrayContent(buffer); result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); return result; } else { var content = new ResponseResult<string> { Errors = new List<string>() { "Image not found" }, Result = null, Succeed = true }; result = new HttpResponseMessage(HttpStatusCode.NotFound); result.Content = new ObjectContent<ResponseResult<string>>(content, new JsonMediaTypeFormatter()); return result; } } } }
public VotesServiceTests() { this.picturesList = new List <Picture>(); this.picturesRepo = new Mock <IDeletableEntityRepository <Picture> >(); this.picturesRepo.Setup(x => x.All()).Returns(this.picturesList.AsQueryable()); this.picturesRepo.Setup(x => x.AddAsync(It.IsAny <Picture>())).Callback((Picture picture) => this.picturesList.Add(picture)); this.picturesService = new PicturesService(this.picturesRepo.Object); this.attachmentsList = new List <Attachment>(); this.attachmentsRepo = new Mock <IDeletableEntityRepository <Attachment> >(); this.attachmentsRepo.Setup(x => x.All()).Returns(this.attachmentsList.AsQueryable()); this.attachmentsRepo.Setup(x => x.AddAsync(It.IsAny <Attachment>())).Callback((Attachment att) => this.attachmentsList.Add(att)); this.issueAttachmentsList = new List <IssueAttachment>(); this.issueAttachmentsRepo = new Mock <IRepository <IssueAttachment> >(); this.issueAttachmentsRepo.Setup(x => x.All()).Returns(this.issueAttachmentsList.AsQueryable()); this.issueAttachmentsRepo.Setup(x => x.AddAsync(It.IsAny <IssueAttachment>())).Callback((IssueAttachment issueAtt) => this.issueAttachmentsList.Add(issueAtt)); this.issuesList = new List <Issue>(); this.issuesRepo = new Mock <IDeletableEntityRepository <Issue> >(); this.issuesRepo.Setup(x => x.All()).Returns(this.issuesList.AsQueryable()); this.issuesRepo.Setup(x => x.AddAsync(It.IsAny <Issue>())).Callback((Issue issue) => this.issuesList.Add(issue)); this.issueTagsList = new List <IssueTag>(); this.issueTagsRepo = new Mock <IRepository <IssueTag> >(); this.issueTagsRepo.Setup(x => x.All()).Returns(this.issueTagsList.AsQueryable()); this.issueTagsRepo.Setup(x => x.AddAsync(It.IsAny <IssueTag>())).Callback((IssueTag issueTag) => this.issueTagsList.Add(issueTag)); this.tagsList = new List <Tag>(); this.tagsRepo = new Mock <IRepository <Tag> >(); this.tagsRepo.Setup(x => x.All()).Returns(this.tagsList.AsQueryable()); this.tagsRepo.Setup(x => x.AddAsync(It.IsAny <Tag>())).Callback((Tag tag) => this.tagsList.Add(tag)); this.tagsService = new TagsService(this.tagsRepo.Object); this.issueTagsService = new IssueTagsService(this.issueTagsRepo.Object, new StringOperationsServices(), this.tagsService); this.citizensList = new List <Citizen>(); this.citizensRepo = new Mock <IDeletableEntityRepository <Citizen> >(); this.citizensRepo.Setup(x => x.All()).Returns(this.citizensList.AsQueryable()); this.citizensRepo.Setup(x => x.AddAsync(It.IsAny <Citizen>())).Callback((Citizen citizen) => this.citizensList.Add(citizen)); this.citizensService = new CitizensService(this.citizensRepo.Object); this.addressList = new List <Address>(); this.addressesRepo = new Mock <IDeletableEntityRepository <Address> >(); this.addressesRepo.Setup(x => x.All()).Returns(this.addressList.AsQueryable()); this.addressesRepo.Setup(x => x.AddAsync(It.IsAny <Address>())).Callback((Address address) => this.addressList.Add(address)); this.addressesService = new AddressesService(this.addressesRepo.Object, this.citiesService); this.citiesList = new List <City>(); this.citiesRepo = new Mock <IRepository <City> >(); this.citiesRepo.Setup(x => x.All()).Returns(this.citiesList.AsQueryable()); this.citiesRepo.Setup(x => x.AddAsync(It.IsAny <City>())).Callback((City city) => this.citiesList.Add(city)); this.citiesService = new CitiesService(this.citiesRepo.Object); this.votesList = new List <Vote>(); this.votesRepo = new Mock <IRepository <Vote> >(); this.votesRepo.Setup(x => x.All()).Returns(this.votesList.AsQueryable()); this.votesRepo.Setup(x => x.AllAsNoTracking()).Returns(this.votesList.AsQueryable()); this.votesRepo.Setup(x => x.Delete(It.IsAny <Vote>())).Callback((Vote vote) => this.votesList.Remove(vote)); this.votesRepo.Setup(x => x.AddAsync(It.IsAny <Vote>())).Callback((Vote vote) => this.votesList.Add(vote)); this.issuesService = new IssuesService( this.issuesRepo.Object, this.issueAttachmentsRepo.Object, this.attachmentsRepo.Object, this.citizensRepo.Object, this.citizensService, this.picturesService, this.addressesService, this.issueTagsService); this.votesService = new VotesService(this.votesRepo.Object, this.citizensService, this.issuesService); }
public async Task<HttpResponseMessage> Post(string type, string id) { string imageId = null; string temPath = null; //Checks if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); //Checks if the type is valid PicType picType; if (!Enum.TryParse<PicType>(type, out picType)) throw new HttpResponseException(HttpStatusCode.InternalServerError); string path = HttpContext.Current.Server.MapPath("~/data/pics"); string tempPath = path + "\\temp"; try { using (var picturesService = new PicturesService()) { picturesService.EnsureDirectory(path + "\\temp"); var provider = new MultipartFormDataStreamProvider(tempPath); MultipartFileData fileData = null; // Read the form data and save in the path. await Request.Content.ReadAsMultipartAsync(provider); if (provider.FileData.Count <= 0) { throw new HttpResponseException(HttpStatusCode.NoContent); } fileData = provider.FileData.First(); if (!fileData.Headers.ContentType.Equals(MediaTypeHeaderValue.Parse("image/jpeg")) && !fileData.Headers.ContentType.Equals(MediaTypeHeaderValue.Parse("image/png")) && !fileData.Headers.ContentType.Equals(MediaTypeHeaderValue.Parse("image/gif"))) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } //var fileContent = provider.GetStream(provider.Contents.First(), provider.Contents.First().Headers); temPath = fileData.LocalFileName; imageId = picturesService.Save(picType, id, fileData.LocalFileName, path); //Adds the image to the corresponding document switch (picType) { //TODO:Implementar el codigo para los demás tipos case PicType.reports: using (var reportsService = new ReportsService()) { var report = reportsService.GetById(id); if(string.IsNullOrEmpty(report.picture)){ report.picture = imageId; }else{ report.detail.pics.Add(imageId); } reportsService.Update(report); }; break; case PicType.users: break; } } } catch (System.Exception e) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } finally { //deletes the temp file if (temPath != null && File.Exists(temPath)) { using (var picturesService = new PicturesService()) { picturesService.DeleteFile(temPath); } } } if (!string.IsNullOrEmpty(imageId)) { HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); imageId = Newtonsoft.Json.JsonConvert.SerializeObject(new { id = imageId }); result.Content = new ObjectContent(imageId.GetType(), imageId, GlobalConfiguration.Configuration.Formatters.JsonFormatter); return result; //return Request.CreateResponse(HttpStatusCode.OK); } else { return Request.CreateResponse(HttpStatusCode.InternalServerError); } }
public IssuesServiceTests() { this.mockFile = new Mock <IFormFile>(); var content = "Hello from my fake file"; var fileName = "Pic.jpg"; var ms = new MemoryStream(); var writer = new StreamWriter(ms); writer.Write(content); writer.Flush(); ms.Position = 0; this.mockFile.Setup(_ => _.OpenReadStream()).Returns(ms); this.mockFile.Setup(_ => _.FileName).Returns(fileName); this.mockFile.Setup(_ => _.Length).Returns(ms.Length); this.picturesList = new List <Picture>(); this.picturesRepo = new Mock <IDeletableEntityRepository <Picture> >(); this.picturesRepo.Setup(x => x.All()).Returns(this.picturesList.AsQueryable()); this.picturesRepo.Setup(x => x.AddAsync(It.IsAny <Picture>())).Callback((Picture picture) => this.picturesList.Add(picture)); this.picturesService = new PicturesService(this.picturesRepo.Object); this.attachmentsList = new List <Attachment>(); this.attachmentsRepo = new Mock <IDeletableEntityRepository <Attachment> >(); this.attachmentsRepo.Setup(x => x.All()).Returns(this.attachmentsList.AsQueryable()); this.attachmentsRepo.Setup(x => x.AddAsync(It.IsAny <Attachment>())).Callback((Attachment att) => this.attachmentsList.Add(att)); this.issueAttachmentsList = new List <IssueAttachment>(); this.issueAttachmentsRepo = new Mock <IRepository <IssueAttachment> >(); this.issueAttachmentsRepo.Setup(x => x.All()).Returns(this.issueAttachmentsList.AsQueryable()); this.issueAttachmentsRepo.Setup(x => x.AddAsync(It.IsAny <IssueAttachment>())).Callback((IssueAttachment issueAtt) => this.issueAttachmentsList.Add(issueAtt)); this.issuesList = new List <Issue>(); this.issuesRepo = new Mock <IDeletableEntityRepository <Issue> >(); this.issuesRepo.Setup(x => x.All()).Returns(this.issuesList.AsQueryable()); this.issuesRepo.Setup(x => x.AllAsNoTracking()).Returns(this.issuesList.AsQueryable()); this.issuesRepo.Setup(x => x.AddAsync(It.IsAny <Issue>())).Callback((Issue issue) => this.issuesList.Add(issue)); this.issueTagsList = new List <IssueTag>(); this.issueTagsRepo = new Mock <IRepository <IssueTag> >(); this.issueTagsRepo.Setup(x => x.All()).Returns(this.issueTagsList.AsQueryable()); this.issueTagsRepo.Setup(x => x.AddAsync(It.IsAny <IssueTag>())).Callback((IssueTag issueTag) => this.issueTagsList.Add(issueTag)); this.tagsList = new List <Tag>(); this.tagsRepo = new Mock <IRepository <Tag> >(); this.tagsRepo.Setup(x => x.All()).Returns(this.tagsList.AsQueryable()); this.tagsRepo.Setup(x => x.AddAsync(It.IsAny <Tag>())).Callback((Tag tag) => this.tagsList.Add(tag)); this.tagsService = new TagsService(this.tagsRepo.Object); this.issueTagsService = new IssueTagsService(this.issueTagsRepo.Object, new StringOperationsServices(), this.tagsService); this.citizensList = new List <Citizen>(); this.citizensRepo = new Mock <IDeletableEntityRepository <Citizen> >(); this.citizensRepo.Setup(x => x.All()).Returns(this.citizensList.AsQueryable()); this.citizensRepo.Setup(x => x.AllAsNoTracking()).Returns(this.citizensList.AsQueryable()); this.citizensRepo.Setup(x => x.AddAsync(It.IsAny <Citizen>())).Callback((Citizen citizen) => this.citizensList.Add(citizen)); this.citizensService = new CitizensService(this.citizensRepo.Object); this.citiesList = new List <City>(); this.citiesRepo = new Mock <IRepository <City> >(); this.citiesRepo.Setup(x => x.All()).Returns(this.citiesList.AsQueryable()); this.citiesRepo.Setup(x => x.AddAsync(It.IsAny <City>())).Callback((City city) => this.citiesList.Add(city)); this.citiesService = new CitiesService(this.citiesRepo.Object); this.addressList = new List <Address>(); this.addressesRepo = new Mock <IDeletableEntityRepository <Address> >(); this.addressesRepo.Setup(x => x.All()).Returns(this.addressList.AsQueryable()); this.addressesRepo.Setup(x => x.AddAsync(It.IsAny <Address>())).Callback((Address address) => this.addressList.Add(address)); this.addressesService = new AddressesService(this.addressesRepo.Object, this.citiesService); this.issuesService = new IssuesService( this.issuesRepo.Object, this.issueAttachmentsRepo.Object, this.attachmentsRepo.Object, this.citizensRepo.Object, this.citizensService, this.picturesService, this.addressesService, this.issueTagsService); }
public PicturesModel(PicturesService pics) { _pics = pics; }