public PhotoDTO[] GetPhotos(string albumId)
        {
            int id;

            if (int.TryParse(albumId, out id))
            {
                using (var db = new PhotosContext())
                {
                    var photos = (from a in db.Albums//.Include(p => p.Photos)
                                  where a.Id == id
                                  select a.Photos.Where(p => p.Dimension == null)).FirstOrDefault();

                    if (photos != null)
                    {
                        var arrayPhotos = photos.Select(a => new PhotoDTO
                        {
                            Id       = a.Id,
                            Name     = a.Name,
                            Comments = a.Comments,
                            Link     = new Uri(string.Format(@"http://www.riscanet.com/Service/MediaService.svc/GetPhoto/{0}/{1}?apikey={2}",
                                                             albumId, a.Id, HttpContext.Current.Request.QueryString["apikey"]))
                        })
                                          .ToArray();
                        return(arrayPhotos);
                    }
                }
            }
            return(null);
        }
        public Stream GetPhoto(string albumId, string photoId)
        {
            int albumid, photoid;

            if (int.TryParse(albumId, out albumid) && int.TryParse(photoId, out photoid))
            {
                using (var db = new PhotosContext())
                {
                    var photo = (from a in db.Albums//.Include(p => p.Photos)
                                 where a.Id == albumid
                                 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);
                            if (File.Exists(fullPath))
                            {
                                FileStream fs = File.OpenRead(fullPath);
                                WebOperationContext.Current.OutgoingResponse.ContentType = GetContentTypeFromFileName(photo.Name);
                                return(fs);
                            }
                        }
                    }
                }
            }
            WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
            return(null);
        }
 public AlbumDTO[] GetAlbums()
 {
     using (var db = new PhotosContext())
         return(db.Albums.ToList().Select(a => new AlbumDTO
         {
             Id = a.Id,
             Name = a.Name,
             Comments = a.Comments,
             Link = new Uri(string.Format(@"http://www.riscanet.com/Service/MediaService.svc/GetPhotos/{0}?apikey={1}",
                                          a.Id, HttpContext.Current.Request.QueryString["apikey"]))
         })
                .ToArray());
 }
        protected static async Task <TResult> PerformDatabaseFunction <TResult>(Func <PhotosContext, Task <TResult> > databaseFunction)
        {
            using var connection = new PhotosContext();

            try
            {
                var result = await databaseFunction(connection).ConfigureAwait(false);

                await connection.SaveChangesAsync().ConfigureAwait(false);

                return(result);
            }
            catch (Exception e)
            {
                Debug.WriteLine("");
                Debug.WriteLine(e.Message);
                Debug.WriteLine(e.ToString());
                Debug.WriteLine("");

                throw;
            }
        }
Exemple #5
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, PhotosContext photosContext)
        {
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            /*  photosContext.Database.EnsureDeleted();
             * DbInitialize.Initialize(photosContext);*/
        }
Exemple #6
0
 public EFPhotosRepository()
 {
     this.context = new PhotosContext();
 }
        public Stream GetPhotoResized(string albumId, string photoId, string dimension)
        {
            int    albumid, photoid;
            double size;

            if (int.TryParse(albumId, out albumid) && int.TryParse(photoId, out photoid) && double.TryParse(dimension, out size))
            {
                using (var db = new PhotosContext())
                {
                    var photos = (from a in db.Albums//.Include(p => p.Photos)
                                  where a.Id == albumid
                                  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 == size
                                                    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)size).ToString(), System.IO.Path.GetExtension(photo.Name)));

                                        ImageResizer.ResizeImage(System.IO.Path.Combine(photo.Path, photo.Name), newPhotoPath, size, 50);
                                        photo.ResizedPhotos.Add(new Photo {
                                            Name = string.Format("{0}-{1}{2}", System.IO.Path.GetFileNameWithoutExtension(photo.Name), ((int)size).ToString(), System.IO.Path.GetExtension(photo.Name)), Path = photo.Path, Dimension = size
                                        });
                                        db.SaveChanges();
                                    }
                                }
                                else
                                {
                                    var newPhotoFileName = string.Format("{0}-{1}{2}", System.IO.Path.GetFileNameWithoutExtension(photo.Name), ((int)size).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, size, 50);
                                    photo.ResizedPhotos.Add(new Photo {
                                        Name = newPhotoFileName, Path = photo.Path, Dimension = size
                                    });
                                    db.SaveChanges();
                                }

                                FileStream fs = File.OpenRead(fullPath);
                                WebOperationContext.Current.OutgoingResponse.ContentType = GetContentTypeFromFileName(photo.Name);
                                return(fs);
                            }
                        }
                    }
                }
            }
            WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
            return(null);
        }
Exemple #8
0
 public PhotosController(PhotosContext context, IPhotoStorage photoStorage, IBus bus)
 {
     _context      = context;
     _photoStorage = photoStorage;
     _bus          = bus;
 }
Exemple #9
0
 public PhotosController(PhotosContext context)
 {
     _context = context;
 }
Exemple #10
0
        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);
            }
        }