public HttpResponseMessage Get(int id) { using (var db = new VideoContext()) { var response = Request.CreateResponse(); var video = (from a in db.Videos where a.Id == id select a).FirstOrDefault(); if (video != null) { using (new NetworkConnection()) { var fullPath = video.Path; var mediaStreaming = new MediaStreaming(fullPath); response.Content = new PushStreamContent(mediaStreaming.WriteToStream, new MediaTypeHeaderValue(MediaContentHelper.GetContentTypeFromFileName(video.Path))); } } return(response); } }
public HttpResponseMessage Get(int id, int photoId, int dimension = 0) { using (var db = new PhotosContext()) { var response = Request.CreateResponse(); if (dimension == 0) { var photo = (from a in db.Albums//.Include(p => p.Photos) where a.Id == id select a.Photos.FirstOrDefault(p => p.Id == photoId && p.OriginalPhotoId == null)).FirstOrDefault(); if (photo != null) { using (new NetworkConnection()) { var fullPath = System.IO.Path.Combine(photo.Path, photo.Name); var mediaStreaming = new MediaStreaming(fullPath); response.Content = new PushStreamContent(mediaStreaming.WriteToStream, new MediaTypeHeaderValue(MediaContentHelper.GetContentTypeFromFileName(photo.FullPath))); } } } else { var photos = (from a in db.Albums//.Include(p => p.Photos) where a.Id == id select a.Photos).FirstOrDefault(); if (photos != null) { var photo = (from p in photos where p.Id == photoId && p.OriginalPhotoId == null && p.Dimension == null select p).FirstOrDefault(); if (photo != null) { if (photo.ResizedPhotos == null) { photo.ResizedPhotos = new List <Photo>(); } using (new NetworkConnection()) { var resizedPhoto = (from p in db.Photos where p.OriginalPhotoId == photoId && p.Dimension.HasValue && p.Dimension.Value == dimension select p).FirstOrDefault(); string fullPath = string.Empty; if (resizedPhoto != null) { fullPath = System.IO.Path.Combine(resizedPhoto.Path, resizedPhoto.Name); if (!File.Exists(fullPath)) { var newPhotoPath = System.IO.Path.Combine(photo.Path, string.Format("{0}-{1}{2}", System.IO.Path.GetFileNameWithoutExtension(photo.Name), ((int)dimension).ToString(), System.IO.Path.GetExtension(photo.Name))); ImageResizer.ResizeImage(System.IO.Path.Combine(photo.Path, photo.Name), newPhotoPath, dimension, 50); photo.ResizedPhotos.Add(new Photo { Name = string.Format("{0}-{1}{2}", System.IO.Path.GetFileNameWithoutExtension(photo.Name), dimension.ToString(), System.IO.Path.GetExtension(photo.Name)), Path = photo.Path, Dimension = dimension }); db.SaveChanges(); } } else { var newPhotoFileName = string.Format("{0}-{1}{2}", System.IO.Path.GetFileNameWithoutExtension(photo.Name), dimension.ToString(), System.IO.Path.GetExtension(photo.Name)); fullPath = System.IO.Path.Combine(photo.Path, newPhotoFileName); ImageResizer.ResizeImage(System.IO.Path.Combine(photo.Path, photo.Name), fullPath, dimension, 50); photo.ResizedPhotos.Add(new Photo { Name = newPhotoFileName, Path = photo.Path, Dimension = dimension }); db.SaveChanges(); } var mediaStreaming = new MediaStreaming(fullPath); response.Content = new PushStreamContent(mediaStreaming.WriteToStream, new MediaTypeHeaderValue(MediaContentHelper.GetContentTypeFromFileName(photo.FullPath))); } } } } return(response); } }