using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Web; public class PhotoManager { private SqlConnection connection; private SqlCommand command; private bool filter; public PhotoManager() { // Io personalmente uso teniche diverse per centralizzare Connection e Command // evitando di definire connection e command nel costruttore. connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString); command = new SqlCommand(); command.Connection = connection; command.CommandType = CommandType.StoredProcedure; filter = true; if (HttpContext.Current.User.IsInRole("Friends") || HttpContext.Current.User.IsInRole("Administrators")) { filter = false; } } // Photo-Related Methods public Stream GetPhoto(int photoid, PhotoSize size) { command.CommandText = "GetPhoto"; // Mancano i tipi di parametro command.Parameters.Add(new SqlParameter("@PhotoID", photoid)); command.Parameters.Add(new SqlParameter("@Size", (int)size)); command.Parameters.Add(new SqlParameter("@IsPublic", filter)); object result; // Non farei result come object ma come variabile tipizzata per evitare il cast // Inserito Try Catch try { try { connection.Open(); result = command.ExecuteScalar(); } finally { connection.Close(); } } catch (SqlException ex) { return null; } try { return new MemoryStream((byte[])result); } catch (ArgumentNullException e) { return null; } } public Stream GetPhoto(PhotoSize size) { string path = HttpContext.Current.Server.MapPath("~/Images/"); switch (size) { case PhotoSize.Small: path += "placeholder-100.jpg"; break; case PhotoSize.Medium: path += "placeholder-200.jpg"; break; case PhotoSize.Large: path += "placeholder-600.jpg"; break; default: path += "placeholder-600.jpg"; break; } return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); } public Stream GetFirstPhoto(int albumid, PhotoSize size) { command.CommandText = "GetFirstPhoto"; // Mancano i tipi di parametri command.Parameters.Add(new SqlParameter("@AlbumID", albumid)); command.Parameters.Add(new SqlParameter("@Size", (int)size)); command.Parameters.Add(new SqlParameter("@IsPublic", filter)); object result; // Vedi sopra per Object try { try { connection.Open(); result = command.ExecuteScalar(); } finally { connection.Close(); } } catch (SqlException ex) { return null; } // Per non stravolgere tutto ho lasciato staccato questo secondo Try/Catch try { return new MemoryStream((byte[])result); } catch (ArgumentNullException e) { return null; } } public List GetPhotos(int AlbumID) { command.CommandText = "GetPhotos"; command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID)); command.Parameters.Add(new SqlParameter("@IsPublic", filter)); SqlDataReader reader; try { connection.Open(); reader = command.ExecuteReader(); } catch (SqlException ex) { return null; } List list = new List(); while (reader.Read()) { Photo temp = new Photo( (int)reader["PhotoID"], (int)reader["AlbumID"], (string)reader["Caption"]); list.Add(temp); } // Chiudo la connessione !!! connection.Close(); return list; } public List GetPhotos() { return GetPhotos(GetRandomAlbumID()); } public void AddPhoto(int AlbumID, string Caption, byte[] BytesOriginal) { command.CommandText = "AddPhoto"; command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID)); command.Parameters.Add(new SqlParameter("@Caption", Caption)); command.Parameters.Add(new SqlParameter("@BytesOriginal", BytesOriginal)); command.Parameters.Add(new SqlParameter("@BytesFull", ResizeImageFile(BytesOriginal, 600))); command.Parameters.Add(new SqlParameter("@BytesPoster", ResizeImageFile(BytesOriginal, 198))); command.Parameters.Add(new SqlParameter("@BytesThumb", ResizeImageFile(BytesOriginal, 100))); // Manca gestione dell'errore ?!?!? // Non si sa cosa restituire al chiamante // In origine non si faceva catch e quindi non l'ho fatto. // object result = command.ExecuteNonQuery(); connection.Open(); command.ExecuteNonQuery(); connection.Close(); } public void RemovePhoto(int original_PhotoID) { command.CommandText = "RemovePhoto"; command.Parameters.Add(new SqlParameter("@PhotoID", original_PhotoID)); // Aggiunto Open e Close prima della Execute // Anche sui metodi successivi // Manca gestione dell'errore ?!?!? // Non si sa cosa restituire al chiamante // In origine non si faceva catch e quindi non l'ho fatto. // Ho aggiunto almeno il finally per chiudere try { connection.Open(); command.ExecuteNonQuery(); } finally { connection.Close(); } } public void EditPhoto(string Caption, int original_PhotoID) { command.CommandText = "EditPhoto"; command.Parameters.Add(new SqlParameter("@Caption", Caption)); command.Parameters.Add(new SqlParameter("@original_PhotoID", original_PhotoID)); // Aggiunto Open e Close prima della Execute // Anche sui metodi successivi // Manca gestione dell'errore ?!?!? // Non si sa cosa restituire al chiamante // In origine non si faceva catch e quindi non l'ho fatto. // Ho aggiunto almeno il finally per chiudere try { connection.Open(); command.ExecuteNonQuery(); } finally { connection.Close(); } } // Album-Related Methods public List GetAlbums() { command.CommandText = "GetAlbums"; command.Parameters.Add(new SqlParameter("@IsPublic", filter)); List list = new List(); // Aggiunto il finally try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Album temp = new Album( (int)reader["AlbumID"], (int)reader["NumberOfPhotos"], (string)reader["Caption"], (bool)reader["IsPublic"]); list.Add(temp); } } finally { // Aggiunto Open e Close connection.Close(); ; } return list; } public void AddAlbum(string Caption, bool IsPublic) { command.CommandText = "AddAlbum"; command.Parameters.Add(new SqlParameter("@Caption", Caption)); command.Parameters.Add(new SqlParameter("@IsPublic", IsPublic)); // Aggiunto Open e Close prima della Execute // Anche sui metodi successivi // Manca gestione dell'errore ?!?!? // Non si sa cosa restituire al chiamante // In origine non si faceva catch e quindi non l'ho fatto. // Ho aggiunto almeno il finally per chiudere try { connection.Open(); command.ExecuteNonQuery(); } finally { connection.Close(); } } public void RemoveAlbum(int original_AlbumID) { command.CommandText = "RemoveAlbum"; command.Parameters.Add(new SqlParameter("@original_AlbumID", original_AlbumID)); // Aggiunto Open e Close prima della Execute // Anche sui metodi successivi // Manca gestione dell'errore ?!?!? // Non si sa cosa restituire al chiamante // In origine non si faceva catch e quindi non l'ho fatto. // Ho aggiunto almeno il finally per chiudere try { connection.Open(); command.ExecuteNonQuery(); } finally { connection.Close(); } } public void EditAlbum(string Caption, bool IsPublic, int original_AlbumID) { command.CommandText = "EditAlbum"; command.Parameters.Add(new SqlParameter("@Caption", Caption)); command.Parameters.Add(new SqlParameter("@IsPublic", IsPublic)); command.Parameters.Add(new SqlParameter("@original_AlbumID", original_AlbumID)); // Aggiunto Open e Close prima della Execute // Anche sui metodi successivi // Manca gestione dell'errore ?!?!? // Non si sa cosa restituire al chiamante // In origine non si faceva catch e quindi non l'ho fatto. // Ho aggiunto almeno il finally per chiudere try { connection.Open(); command.ExecuteNonQuery(); } finally { connection.Close(); } } public int GetRandomAlbumID() { // Aggiunto Try/Finally List list = new List(); command.CommandText = "GetNonEmptyAlbums"; SqlDataReader reader; try { connection.Open(); reader = command.ExecuteReader(); while (reader.Read()) { Album temp = new Album((int)reader["AlbumID"], 0, "", false); list.Add(temp); } } finally { // reader.Close(); connection.Close(); } // Per non stravolgere tutto ho lasciato staccato questo secondo Try/Catch try { Random r = new Random(); return list[r.Next(list.Count)].AlbumID; } catch (ArgumentOutOfRangeException e) { return -1; } } // Auxiliary Functions private byte[] ResizeImageFile(byte[] imageFile, int targetSize) { System.Drawing.Image original = System.Drawing.Image.FromStream(new MemoryStream(imageFile)); int targetH, targetW; if (original.Height > original.Width) { targetH = targetSize; targetW = (int)(original.Width * ((float)targetSize / (float)original.Height)); } else { targetW = targetSize; targetH = (int)(original.Height * ((float)targetSize / (float)original.Width)); } System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new MemoryStream(imageFile)); // Create a new blank canvas. The resized image will be drawn on this canvas. Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb); bmPhoto.SetResolution(72, 72); Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.SmoothingMode = SmoothingMode.AntiAlias; grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality; grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel); // Save out to memory and then to a file. We dispose of all objects to make sure the files don't stay locked. MemoryStream mm = new MemoryStream(); bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg); original.Dispose(); imgPhoto.Dispose(); bmPhoto.Dispose(); grPhoto.Dispose(); return mm.GetBuffer(); } public ICollection ListUploadDirectory() { DirectoryInfo d = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("~/Upload")); return d.GetFileSystemInfos("*.jpg"); } }