/// <summary> /// load context for unworked image file from previous sessions /// </summary> /// <returns></returns> public List <TableItem> DataInitialization() { var rowList = new List <TableItem>(); const string strSQL = "Select * From Images Where Status = 1 Or Status = 3 "; using (var con = new OleDbConnection(_connectionString)) using (var command = new OleDbCommand(strSQL, con)) { con.Open(); using (OleDbDataReader reader = command.ExecuteReader()) { while (reader.Read()) { var item = new TableItem { DateTimeItem = (DateTime)reader[1], Id = UInt32.Parse(reader[0].ToString()), SizeBefore = UInt32.Parse(reader[2].ToString()), SizeAfter = UInt32.Parse(reader[3].ToString()), StatusItem = (TableItem.StatusType)Int16.Parse(reader[4].ToString()), FileName = new FileInfo(reader[5].ToString()), KrakenURL = reader[6].ToString(), ItemInProcess = false }; rowList.Add(item); } } con.Close(); } return(rowList); }
/// <summary> /// delete row from table /// </summary> /// <param name="fileName"></param> public void DeleteRow(string fileName) { TableItem itemToRemove = RowItem.Single(r => r.FileName.FullName == fileName); if (itemToRemove != null) { RowItem.Remove(itemToRemove); _bService.DeleteImadgeData(fileName); } }
/// <summary> /// change row context /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ChangeRow(object sender, TableItem e) { DataGridViewRow row = dataGridView1.Rows.Cast <DataGridViewRow>().FirstOrDefault(r => r.Cells[0].Value.ToString().Equals(e.FileName.FullName)); if (row != null) { row.Cells[2].Value = e.SizeAfter.ToString(CultureInfo.InvariantCulture); row.Cells[3].Value = e.StatusItem.ToString(); if (e.Description != null) { row.Cells[4].Value = e.Description; } } }
/// <summary> /// update context for image file /// </summary> /// <param name="item"></param> public void UpdateImageData(TableItem item) { using (var con = new OleDbConnection(_connectionString)) using (OleDbCommand cmd = con.CreateCommand()) { cmd.CommandText = @"update Images set SizeB = @SizeB, SizeA = @SizeA, Status = @Status, KrakenU = @KrakenU where PathF = @PathF"; cmd.Parameters.Add(new OleDbParameter("@SizeB", item.SizeBefore)); cmd.Parameters.Add(new OleDbParameter("@SizeA", item.SizeAfter)); cmd.Parameters.Add(new OleDbParameter("@Status", item.StatusItem)); cmd.Parameters.Add(new OleDbParameter("@KrakenU", item.KrakenURL)); cmd.Parameters.Add(new OleDbParameter("@PathF", item.FileName)); con.Open(); cmd.ExecuteNonQuery(); } }
/// <summary> /// save new image file data /// </summary> /// <param name="row"></param> public void SaveNewData(TableItem row) { using (var con = new OleDbConnection(_connectionString)) using (OleDbCommand cmd = con.CreateCommand()) { cmd.CommandText = @"Insert INTO Images ([Date], SizeB, SizeA, Status, PathF, KrakenU) VALUES (@Date, @SizeB, @SizeA, @Status, @PathF, @KrakenU)"; cmd.Parameters.Add(new OleDbParameter("@Date", row.DateTimeItem.ToString("d"))); cmd.Parameters.Add(new OleDbParameter("@SizeB", row.SizeBefore)); cmd.Parameters.Add(new OleDbParameter("@SizeA", row.SizeAfter)); cmd.Parameters.Add(new OleDbParameter("@Status", row.StatusItem)); cmd.Parameters.Add(new OleDbParameter("@PathF", row.FileName.FullName)); cmd.Parameters.Add(new OleDbParameter("@KrakenU", row.KrakenURL)); con.Open(); cmd.ExecuteNonQuery(); } }
/// <summary> /// add new image files to table by draging /// </summary> /// <param name="filename"></param> public void AddNewByDraging(string filename) { if (!HaveThisFileName(filename)) { var item = new TableItem(filename); var row = new List <TableItem>(); item.KrakenURL = ""; row.Add(item); RowItem.Add(item); _bService.SaveNewData(item); AddingRows?.Invoke(this, row); } else { Messaging?.Invoke(this, "One or more file(s) already in use. Remove first it(s) if need"); } if (!_krakenIsStarting) { Task.Run(() => GetStartedWithKraken()); } }
/// <summary> /// make optimization /// </summary> private void GetStartedWithKraken() { InitializationSettings(); if (Settings["keyAPI"] == "Type your - api - key" || Settings["secret"] == "Type your-api-secret") { Messaging?.Invoke(this, "Plese set you key and(or) secret key for kraken.io"); return; } _krakenIsStarting = true; int currentProcessUnloading = 0; int currentProcessDownload = 0; while (true) { int countSetDownloading = Int32.Parse(Settings["Threads"]); int countSetUploading = Int32.Parse(Settings["Threads"]); if (currentProcessDownload <= countSetDownloading) { if (RowItem == null) { return; } TableItem item = RowItem.FirstOrDefault(it => it.StatusItem == TableItem.StatusType.Sended && !it.ItemInProcess); if (item != null) { item.ItemInProcess = true; Interlocked.Increment(ref currentProcessDownload); string selectedFile = item.FileName.FullName; var worker = new Worker(Settings["keyAPI"], Settings["secret"], selectedFile, Parametrs) { KrakenURL = item.KrakenURL }; var tasksDownload = new Task(worker.LoadImage); lock (Locker) { if (_isProgramClosing) { return; } item.StatusItem = TableItem.StatusType.Downloading; _bService.UpdateImageData(item); } ChangingRow?.Invoke(this, item); tasksDownload.Start(); tasksDownload.ContinueWith(t => { lock (Locker) { if (_isProgramClosing) { return; } if (worker.ErrorMessage == null) { item.StatusItem = TableItem.StatusType.Downloaded; item.SizeAfter = (uint)(new FileInfo(item.FileName.FullName + ".back")).Length; RewriteFile(item.FileName); } else { item.Description = worker.ErrorMessage; item.StatusItem = TableItem.StatusType.Sended; } item.ItemInProcess = false; _bService.UpdateImageData(item); Interlocked.Decrement(ref currentProcessDownload); ChangingRow?.Invoke(this, item); } }); } } if (currentProcessUnloading < countSetUploading) { TableItem item = RowItem.FirstOrDefault(it => it.StatusItem == TableItem.StatusType.NotSent && !it.ItemInProcess); if (item != null) { item.ItemInProcess = true; Interlocked.Increment(ref currentProcessUnloading); string selectedFile = item.FileName.FullName; var worker = new Worker(Settings["keyAPI"], Settings["secret"], selectedFile, Parametrs); var tasksUpload = new Task(worker.SendImage); lock (Locker) { if (_isProgramClosing) { return; } item.StatusItem = TableItem.StatusType.Sending; _bService.UpdateImageData(item); } ChangingRow?.Invoke(this, item); tasksUpload.Start(); tasksUpload.ContinueWith((t) => { lock (Locker) { if (_isProgramClosing) { return; } if (worker.KrakenURL != null) { item.KrakenURL = worker.KrakenURL; item.StatusItem = TableItem.StatusType.Sended; } else { item.Description = worker.ErrorMessage; item.StatusItem = TableItem.StatusType.NotSent; } item.ItemInProcess = false; _bService.UpdateImageData(item); Interlocked.Decrement(ref currentProcessUnloading); ChangingRow? . Invoke(this, item); } }); } } Thread.Sleep(5); Application.DoEvents(); InitializationSettings(); } }