/// <summary> /// See interface docs. /// </summary> /// <param name="directoryCache"></param> /// <param name="icao24"></param> /// <param name="registration"></param> /// <returns></returns> public PictureDetail FindPicture(IDirectoryCache directoryCache, string icao24, string registration) { PictureDetail result = null; var fileName = GetImageFileName(directoryCache, icao24, registration); if (!String.IsNullOrEmpty(fileName)) { var fileInfo = new FileInfo(fileName); if (fileInfo != null) { var imageDimensionsFetcher = Factory.Singleton.Resolve <IImageDimensionsFetcher>(); var size = imageDimensionsFetcher.ReadDimensions(fileName); result = new PictureDetail() { FileName = fileName, Width = size.Width, Height = size.Height, LastModifiedTime = fileInfo.LastWriteTimeUtc, Length = fileInfo.Length, }; } } return(result); }
public void PictureDetail_Constructor_Initialises_To_Known_State_And_Properties_Work() { var detail = new PictureDetail(); TestUtilities.TestProperty(detail, r => r.FileName, null, "Ha"); TestUtilities.TestProperty(detail, r => r.Height, 0, 12003); TestUtilities.TestProperty(detail, r => r.Width, 0, 2013); TestUtilities.TestProperty(detail, r => r.LastModifiedTime, default(DateTime), DateTime.UtcNow); TestUtilities.TestProperty(detail, r => r.Length, 0L, long.MaxValue); }
private PictureDetail GetFakePictureDetail(string imageFullPath, int width, int height, DateTime?lastModifiedTime) { PictureDetail result = null; var bytes = _FileSystem.FileExists(imageFullPath) ? _FileSystem.FileReadAllBytes(imageFullPath) : null; if (bytes != null) { result = new PictureDetail() { FileName = imageFullPath, Height = height, Width = width, Length = bytes.Length, LastModifiedTime = lastModifiedTime == null ? DateTime.UtcNow : lastModifiedTime.Value, }; } return(result); }
public void ProcessPictures(DataSet ds, string TableName, string path, string filename) { PictureDetail pictureDetail = new PictureDetail(); RootPicture rootPicture = new RootPicture(); string[] PicArray = new string[ds.Tables[0].Rows.Count]; ObjPicture objPicture = new ObjPicture(); string fullpath = @path + @"\" + filename; fullpath = fullpath.Substring(6); long xCntr = 1; // delete existing if (File.Exists(fullpath)) { File.Delete(fullpath); } string output2 = JsonConvert.SerializeObject(ds, Formatting.Indented); File.AppendAllText(fullpath, output2); }
public void TestInitialise() { _OriginalFactory = Factory.TakeSnapshot(); _RuntimeEnvironment = TestUtilities.CreateMockSingleton <IRuntimeEnvironment>(); _RuntimeEnvironment.Setup(r => r.IsTest).Returns(true); _Clock = new ClockMock(); Factory.RegisterInstance <IClock>(_Clock.Object); _Fetcher = Factory.ResolveNewInstance <IAircraftDetailFetcher>(); _FetchedHandler = new EventRecorder <EventArgs <AircraftDetail> >(); _Fetcher.Fetched += _FetchedHandler.Handler; _Aircraft = TestUtilities.CreateMockInstance <IAircraft>(); _Aircraft.Setup(r => r.Icao24).Returns("ABC123"); // The fetcher uses a private heartbeat service to avoid slowing the GUI down. Unfortunately // the TestUtilities don't support creating non-singleton instances of ISingletons, do we // have to do it manually. _Heartbeat = TestUtilities.CreateMockInstance <IHeartbeatService>(); Factory.RegisterInstance <IHeartbeatService>(_Heartbeat.Object); _AutoConfigDatabase = TestUtilities.CreateMockSingleton <IAutoConfigBaseStationDatabase>(); _Database = TestUtilities.CreateMockInstance <IBaseStationDatabase>(); _AutoConfigDatabase.Setup(r => r.Database).Returns(_Database.Object); _DatabaseAircraftAndFlights = null; _GetManyAircraftAndFlightsByCodes = new List <string>(); _GetManyAircraftByCodes = new List <string>(); _Database.Setup(r => r.GetManyAircraftByCode(It.IsAny <IEnumerable <string> >())).Returns((IEnumerable <string> icaos) => { var result = new Dictionary <string, BaseStationAircraft>(); if (icaos != null && icaos.Count() == 1 && icaos.First() == "ABC123" && _DatabaseAircraft != null) { result.Add("ABC123", _DatabaseAircraft); } if (icaos != null) { _GetManyAircraftByCodes.AddRange(icaos); } return(result); }); _Database.Setup(r => r.GetManyAircraftAndFlightsCountByCode(It.IsAny <IEnumerable <string> >())).Returns((IEnumerable <string> icaos) => { var result = new Dictionary <string, BaseStationAircraftAndFlightsCount>(); if (icaos != null && icaos.Count() == 1 && icaos.First() == "ABC123" && _DatabaseAircraftAndFlights != null) { result.Add("ABC123", _DatabaseAircraftAndFlights); } if (icaos != null) { _GetManyAircraftAndFlightsByCodes.AddRange(icaos); } return(result); }); _AutoConfigPictureFolderCache = TestUtilities.CreateMockSingleton <IAutoConfigPictureFolderCache>(); _PictureFolderCache = TestUtilities.CreateMockInstance <IDirectoryCache>(); _AutoConfigPictureFolderCache.Setup(r => r.DirectoryCache).Returns(() => _PictureFolderCache.Object); _AircraftPictureManager = TestUtilities.CreateMockSingleton <IAircraftPictureManager>(); _PictureManagerCache = _PictureFolderCache.Object; _PictureManagerIcao24 = "INVALID"; _PictureManagerReg = null; _PictureManagerThrowException = false; _PictureDetail = new PictureDetail(); _AircraftPictureManager.Setup(r => r.FindPicture(It.IsAny <IDirectoryCache>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <PictureDetail>())).Returns((IDirectoryCache cache, string icao24, string reg, PictureDetail existingPictureDetail) => { if (_PictureManagerThrowException) { throw new InvalidOperationException(); } return(cache == _PictureManagerCache && icao24 == _PictureManagerIcao24 && reg == _PictureManagerReg ? _PictureDetail : null); }); _StandingDataManager = TestUtilities.CreateMockSingleton <IStandingDataManager>(); _AircraftType = new AircraftType(); _StandingDataManager.Setup(r => r.FindAircraftType(It.IsAny <string>())).Returns((string type) => { return(type == _FindAircraftType ? _AircraftType : null); }); _Log = TestUtilities.CreateMockSingleton <ILog>(); _Log.Setup(r => r.WriteLine(It.IsAny <string>())).Callback((string x) => { throw new InvalidOperationException(x); }); _Log.Setup(r => r.WriteLine(It.IsAny <string>(), It.IsAny <object[]>())).Callback((string x, object[] args) => { throw new InvalidOperationException(String.Format(x, args)); }); _AircraftOnlineLookupManager = TestUtilities.CreateMockSingleton <IAircraftOnlineLookupManager>(); }
public void PictureDetail_Equals_Returns_True_When_Comparing_Identical_Objects() { var aircraft = new PictureDetail(); Assert.AreEqual(true, aircraft.Equals(aircraft)); }
public void ProcessPicTables(string TableName, PictureBox picBox, Label lblCount, Label lblTotal) { Image imgFromFile = null; bool skip = true; // set file path string fullpath = @constants.SystemPath + @"\"; fullpath = fullpath.Substring(6); // set json file DeleteFile(fullpath, "picture_js.json"); File.AppendAllText(fullpath + "picture_js.json", "[\n"); // set database connections DataSet ds = DSRecords(TableName); if (ds == null) { return; } lblTotal.Text = ds.Tables[0].Rows.Count.ToString(); // loop through table for (int x = 0; x < ds.Tables[0].Rows.Count; x++) { if (x != 0) { if (skip == true) { File.AppendAllText(fullpath + "picture_js.json", ", "); } else { skip = true; } } lblCount.Text = (x + 1).ToString(); lblCount.Refresh(); // delete files DeleteFile(fullpath, "image_temp.jpg"); DeleteFile(fullpath, "image_temp_compressed.jpg"); DeleteFile(fullpath, "base64_un.txt"); DeleteFile(fullpath, "base64_comp.txt"); picBox.Image = null; picBox.Refresh(); // read from database, save uncompressed image to file byte[] blobpic = (byte[])ds.Tables[0].Rows[x]["PicBlob"]; // if invalid image, continue if (blobpic.Length == 0) { skip = false; continue; } // convert uncompressed array to base64, write to text file string base64Text = Convert.ToBase64String(blobpic); File.AppendAllText(fullpath + "base64_un.txt", base64Text); // write uncompressd to image file File.WriteAllBytes(fullpath + "image_temp.jpg", blobpic); // convert to base64 to allow writting to file base64Text = Convert.ToBase64String(blobpic); Image img = Base64ToImage(base64Text); picBox.Image = img; picBox.Refresh(); // read uncompressed image from file try { imgFromFile = Image.FromFile(fullpath + "image_temp.jpg"); } catch (Exception ex) { picBox.Image = null; picBox.Refresh(); MessageBox.Show("Failed to read uncompressed image from file.\n" + ex.Message, "ProcessPicTables"); skip = false; continue; } // compress image bool isCompressed = DefaultCompresion(imgFromFile, fullpath, "image_temp_compressed.jpg"); imgFromFile.Dispose(); // release file resources if (isCompressed == false) { skip = false; return; } // read compressed image in as base64 string ImageAsBase64 = ImageToBase64(fullpath, "image_temp_compressed.jpg"); // write compressed base64 string to text file File.AppendAllText(fullpath + "base64_comp.txt", ImageAsBase64); // crete new object PictureDetail picDetal = new PictureDetail { IDPicture = (int)ds.Tables[0].Rows[x]["IDPictures"], PicName = ds.Tables[0].Rows[x]["PicName"].ToString() }; var idsurgeryval = ds.Tables[0].Rows[x]["IDSurgery"]; if (idsurgeryval.ToString() != "") { picDetal.IDSurgery = (int?)ds.Tables[0].Rows[x]["IDSurgery"]; } else { picDetal.IDSurgery = null; } // TODO - put blob back in json file //picDetal.Image = ImageAsBase64; picDetal.Image = null; // object to file File.AppendAllText(fullpath + "picture_js.json", JsonConvert.SerializeObject(picDetal)); //File.AppendAllText(fullpath + "picture_js.json", ", "); #region Tried This - Delete //// write array to image //Image imgByte = ByteArrayToImage(blobpic); //if (imgByte == null) //{ // continue; //} //// compress/save image //MemoryStream ms = new MemoryStream(); //MemoryStream ms2 = new MemoryStream(); //Image imgConvert = Base64ToImage(base64Text); //if (imgConvert == null) //{ // continue; //} //try //{ // imgConvert.Save(ms2, imgConvert.RawFormat); //} //catch (Exception ex) //{ // MessageBox.Show("Failed to save image to stream ms2: " + ex, "ProcessPicTables"); // continue; //} //try //{ // imgConvert.Save(ms2, ImageFormat.Png); //} //catch (Exception ex) //{ // MessageBox.Show("Failed to save image to stream ms: " + ex, "ProcessPicTables"); // continue; //} #endregion Tried This #region Current Stuff //try //{ // Image imgBase = Base64ToImage(base64Text); // if (imgBase == null) // { // continue; // } // MemoryStream msStream = new MemoryStream(); // imgBase.Save(msStream, ImageFormat.Png); // string imgBaseString = ImgStreamToBase64(imgBase); // File.AppendAllText(fullpath + "base64_comp.txt", imgBaseString); // msStream.Close(); // msStream.Dispose(); //} //catch (Exception ex) //{ // MessageBox.Show("Failed to write compressed file: " + ex, "ProcessPicTables"); //} //try //{ // //ms.Close(); // //ms.Dispose(); // //ms2.Close(); // //ms2.Dispose(); //} //catch //{ //} #endregion #region Comment this out ////// byte array to image ////Image imgcomp = ByteArrayToImage(blobpic); ////// compress image array ////MemoryStream ms = CompressStream(imgcomp); ////// memory stream to base64 ////string compbase64 = StreamToBase64(ms); ////File.AppendAllText(fullpath + "base64_comp.txt", compbase64); //Image ImageFromString = Base64ToImage(base64Text); //// save blob to file //Boolean WriteResult = WriteArrayToFile("image_temp.jpg", blobpic, fullpath); //if (WriteResult == false) //{ // return; //} //// read non-compressed image //byte[] ImageNotCompressed = ReadImageFileToArray(fullpath, "image_temp.jpg"); //if (ImageNotCompressed == null) //{ // return; //} //// convert byte[] to image ////Image ImgFromByte = ByteArrayToImage(blobpic); //Image ImgFromByte = ByteArrayToImage(ImageNotCompressed); //picBox.Image = ImgFromByte; //picBox.SizeMode = PictureBoxSizeMode.StretchImage; //picBox.Refresh(); //if (ImgFromByte == null ) //{ // return; //} //// convert array to base64string //base64Text = Convert.ToBase64String(blobpic); //// compress image //DefaultCompresion(ImgFromByte, fullpath, "image_temp_compressed.jpg"); #endregion } // close json file File.AppendAllText(fullpath + "picture_js.json", "]\n"); }
/// <summary> /// See interface docs. /// </summary> /// <param name="directoryCache"></param> /// <param name="icao24"></param> /// <param name="registration"></param> /// <param name="existingDetail"></param> /// <returns></returns> public PictureDetail FindPicture(IDirectoryCache directoryCache, string icao24, string registration, PictureDetail existingDetail) { var result = existingDetail; if (existingDetail == null) { result = FindPicture(directoryCache, icao24, registration); } else { var fileName = GetImageFileName(directoryCache, icao24, registration); if (String.IsNullOrEmpty(fileName)) { result = null; } else { var fileInfo = new FileInfo(fileName); if (fileInfo == null) { result = null; } else if (fileInfo.LastWriteTimeUtc != existingDetail.LastModifiedTime || fileInfo.Length != existingDetail.Length) { result = FindPicture(directoryCache, icao24, registration); } } } return(result); }