示例#1
0
 public void Init()
 {
     fakeDataSource = new FakeDataSource();
     var photoRecordList = ObjectMother.ReturnPhotoDataRecord(3);
     foreach (var photo in photoRecordList)
     {
         fakeDataSource.Photos.Add(photo);
     }
     fakeDataSource.SaveChanges();
     target = new PhotosController(fakeDataSource, provider);
 }
示例#2
0
        public void Init()
        {
            fakeDataSource = new FakeDataSource();
            var photoRecordList = ObjectMother.ReturnPhotoDataRecord(3);

            foreach (var photo in photoRecordList)
            {
                fakeDataSource.Photos.Add(photo);
            }
            fakeDataSource.SaveChanges();
            target = new PhotosController(fakeDataSource, provider);
        }
示例#3
0
        public HttpResponseMessage Post()
        {
            var returnCode = HttpStatusCode.Created;

            Trace.TraceInformation("Got request to upload photo");
            var data = new Photo();

            data.Id   = Guid.NewGuid();
            data.Path = "originals/" + data.Id.ToString();
            Trace.TraceInformation("PhotoStoragePath = {0}", data.Path);
            var request = ControllerContext.Request;

            if (request.Content == null || request.Content.Headers == null || request.Content.Headers.ContentType == null)
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }
            var contentType = request.Content.Headers.ContentType.MediaType;

            if (contentType == "image/jpeg" || contentType == "image/jpg")
            {
                var imageSize = request.Content.Headers.ContentLength;
                Trace.TraceInformation("ContentLength is {0} bytes", imageSize);
                byte[] imageArray;
                using (var image = request.Content.ReadAsStreamAsync().Result)
                {
                    if (!imageSize.HasValue)
                    {
                        imageSize = (int)image.Length;
                    }
                    var imageSz = (int)imageSize.Value;
                    imageArray = new byte[imageSz];
                    int bytesRead = 0;
                    while (bytesRead < imageSz)
                    {
                        bytesRead += image.Read(imageArray, bytesRead, imageSz - bytesRead);
                    }
                    data.FileSize     = bytesRead;
                    data.LastAccessed = DateTime.Now;
                    Trace.TraceInformation("Getting Exif Data");
                    GetExifData(image, data);


                    image.Close();
                }



                Trace.TraceInformation("Writing photo to storage file {0}", data.Path);
                _storage.WriteFile(data.Path, imageArray);

                data.Server      = request.RequestUri.Host;
                data.CreatedBy   = context.User.Identity.Name;
                data.CreatedDate = DateTime.Now;
                if (returnCode == HttpStatusCode.Created)
                {
                    Trace.TraceInformation("Adding photo data to database");
                    _db.Photos.Add(data);
                    _db.SaveChanges();
                }
            }
            else
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }
            var response = new HttpResponseMessage(returnCode);

            if (returnCode == HttpStatusCode.Created)
            {
                var uri = new Uri("/api/Photos/" + data.Id.ToString(), UriKind.Relative);
                response.Headers.Location = uri;
                var modelData = Mapper.Map <PhotoServer.Domain.Photo, Models.PhotoData>(data);
                response.Content = new ObjectContent <Models.PhotoData>(modelData, new JsonMediaTypeFormatter());
            }
            return(response);
        }