/// <summary> /// Load files in one Folder, then recurse on all contained folders /// </summary> /// <param name="root">Current Top Level Folder Name</param> private void MyLoLoadFolder(DirectoryInfo root) { System.IO.FileInfo[] files = null; System.IO.DirectoryInfo[] subDirs = null; // First, process all the files directly under this folder try { files = root.GetFiles("*.*"); } // This is thrown if even one of the files requires permissions greater // than the application provides. catch (UnauthorizedAccessException e) { // This code just writes out the message and continues to recurse. // You may decide to do something different here. For example, you // can try to elevate your privileges and access the file again. log.Add(e.Message); } catch (System.IO.DirectoryNotFoundException e) { Console.WriteLine(e.Message); } if (files != null) { foreach (System.IO.FileInfo fi in files) { // a try-catch block is required here to handle the case // where the file has been deleted since the call to MyLoLoadFolder(). try { if (fi.Extension == ".jpg" || fi.Extension == ".JPG") { Photo photo = new Photo(); Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); Bitmap image = new Bitmap(fi.FullName); Image thumb = image.GetThumbnailImage(100, 100, myCallback, IntPtr.Zero); ImageConverter converter = new ImageConverter(); photo.Thumbnail = (byte[])converter.ConvertTo(thumb, typeof(byte[])); // This is for testing only - should switch to more reliable Hash such as SHA-256 or MD5 //long CRC = CalculateCRCForFile(fi.FullName); photo.CRC = image.GetHashCode(); image.Dispose(); JpgPhoto photoJpg = new JpgPhoto(fi.FullName); photo.Uri = fi.FullName; photo.Aperture = photoJpg.Metadata.Aperture; photo.Camera = photoJpg.Metadata.CameraModel; photo.DateTaken = photoJpg.Metadata.DateTaken; photo.PhotoIndexKind = _timeIndexKind; DateTime dat = photoJpg.Metadata.DateTaken.Date; TimeSpan tim = photoJpg.Metadata.DateTaken.TimeOfDay; if (photoJpg.Metadata.GpsPosition.Latitude.IsValidCoordinate) { photo.GpsLat = photoJpg.Metadata.GpsPosition.Latitude.Numeric; } if (photoJpg.Metadata.GpsPosition.Longitude.IsValidCoordinate) { photo.GpsLong = photoJpg.Metadata.GpsPosition.Longitude.Numeric; } photo.Uuid = Guid.NewGuid(); //foreach (Tag tag in photo.Metadata.Tags) //{ // Console.WriteLine(tag.FullName); //} long photoId = _myLoStore.AddPhoto(_userId, photo); _count++; // TODO add code to write back GUID and Hash into photo metadata } } catch (MyLoCRCException crcEx) { throw new MyLoException("CRC Calculation Error: Inner exception: " + crcEx); } catch (MyLoDataStoreException dsEx) { throw new MyLoException("DataStore Error: Inner exception: " + dsEx); } catch (Exception ex) { throw new MyLoException("File not Found: " + fi.FullName + " Inner exception: " + ex); } Debug.WriteLine(fi.FullName); } // Now find all the subdirectories under this directory. subDirs = root.GetDirectories(); foreach (System.IO.DirectoryInfo dirInfo in subDirs) { // Resursive call for each subdirectory. MyLoLoadFolder(dirInfo); } } }
/// <summary> /// Adds Photo to MyLo datastore using PostgreSQL function 'addPhoto' /// </summary> /// <param name="userId">An identifier for a MyLoAccount holder</param> /// <param name="...">photo meta data</param> public long AddPhoto(long userId, Photo photo) { try { NpgsqlCommand command = new NpgsqlCommand("AddPhoto", _conn); command.CommandType = CommandType.StoredProcedure; // create 8 parameters for this function for (int i = 0; i < 10; i++) { command.Parameters.Add(new NpgsqlParameter()); } command.Parameters[0].DbType = DbType.Int64; command.Parameters[0].Value = userId; command.Parameters[1].DbType = DbType.String; command.Parameters[1].Value = photo.Uri; command.Parameters[2].DbType = DbType.Guid; command.Parameters[2].Value = photo.Uuid; command.Parameters[3].DbType = DbType.Int64; command.Parameters[3].Value = photo.CRC; command.Parameters[4].DbType = DbType.DateTime; command.Parameters[4].Value = photo.DateTaken; command.Parameters[5].DbType = DbType.String; command.Parameters[5].Value = photo.Camera; command.Parameters[6].DbType = DbType.Double; command.Parameters[6].Value = photo.GpsLat; command.Parameters[7].DbType = DbType.Double; command.Parameters[7].Value = photo.GpsLong; command.Parameters[8].NpgsqlDbType = NpgsqlDbType.Bytea; command.Parameters[8].Value = photo.Thumbnail; command.Parameters[9].DbType = DbType.String; command.Parameters[9].Value = photo.PhotoIndexKind; Object result = command.ExecuteScalar(); return (long)result; } catch (NpgsqlException npex) { _conn.Close(); throw new MyLoDataStoreException(npex.Message, npex); } catch (Exception ex) { _conn.Close(); throw new MyLoDataStoreException(ex.Message); } }