Horizon – Diff between revs 11 and 12

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 11 Rev 12
Line 44... Line 44...
44 "UPDATE \"Snapshots\" SET Data = zeroblob(@dataLength), Hash = @recomputedHash WHERE Hash = @hash"; 44 "UPDATE \"Snapshots\" SET Data = zeroblob(@dataLength), Hash = @recomputedHash WHERE Hash = @hash";
Line 45... Line 45...
45   45  
46 private const string RemoveSnapshotFromHashSql = 46 private const string RemoveSnapshotFromHashSql =
Line -... Line 47...
-   47 "DELETE FROM \"Snapshots\" WHERE Hash = @hash";
-   48  
-   49 private const string GetTransferSnapshotFromHashSql =
-   50 "SELECT \"Name\", \"Path\", \"Time\", \"Data\", \"Shot\", \"Color\", \"Hash\", \"Note\" FROM \"Snapshots\" WHERE Hash = @hash";
-   51  
-   52 private const string SetTransferSnapshotSql =
47 "DELETE FROM \"Snapshots\" WHERE Hash = @hash"; 53 "INSERT INTO \"Snapshots\" ( \"Name\", \"Path\", \"Time\", \"Data\", \"Shot\", \"Color\", \"Hash\", \"Note\" ) VALUES ( @name, @path, @time, zeroblob(@dataLength), zeroblob(@shotLength), @color, @hash, @note )";
48   54  
Line 49... Line 55...
49 private const string RemoveScreenshotFromHashSql = 55 private const string RemoveScreenshotFromHashSql =
50 "UPDATE \"Snapshots\" SET Shot = null WHERE Hash = @hash"; 56 "UPDATE \"Snapshots\" SET Shot = null WHERE Hash = @hash";
Line 374... Line 380...
374 { 380 {
375 _databaseLock.Release(); 381 _databaseLock.Release();
376 } 382 }
377 } 383 }
Line -... Line 384...
-   384  
-   385 public async Task SetCompleteSnapshotAsync(TransferSnapshot transferSnapshot, CancellationToken cancellationToken)
-   386 {
-   387 await _databaseLock.WaitAsync(cancellationToken);
-   388 try
-   389 {
-   390 using (var sqliteConnection =
-   391 new SQLiteConnection(_sqliteConnectionStringBuilder.ConnectionString))
-   392 {
-   393 await sqliteConnection.OpenAsync(cancellationToken);
-   394  
-   395 using (var dbTransaction = sqliteConnection.BeginTransaction())
-   396 {
-   397 try
-   398 {
-   399 using (var dataMemoryStream = new MemoryStream())
-   400 {
-   401 using (var dataZipStream =
-   402 new GZipStream(dataMemoryStream, CompressionMode.Compress, true))
-   403 {
-   404 dataMemoryStream.Position = 0L;
-   405 await dataZipStream.WriteAsync(transferSnapshot.Data, 0,
-   406 transferSnapshot.Data.Length, cancellationToken);
-   407 dataZipStream.Close();
-   408  
-   409 using (var bitmapMemoryStream = new MemoryStream())
-   410 {
-   411 bitmapMemoryStream.Position = 0L;
-   412 using (var bitmapZipStream =
-   413 new GZipStream(bitmapMemoryStream, CompressionMode.Compress,
-   414 true))
-   415 {
-   416 using (var transferImageStream = new MemoryStream(transferSnapshot.Shot))
-   417 {
-   418 transferImageStream.Position = 0L;
-   419 await transferImageStream.CopyToAsync(bitmapZipStream);
-   420 bitmapZipStream.Close();
-   421 bitmapMemoryStream.Position = 0L;
-   422  
-   423 var a = bitmapMemoryStream.ToArray();
-   424  
-   425 // Insert the file change.
-   426 using (var sqliteCommand =
-   427 new SQLiteCommand(SetTransferSnapshotSql, sqliteConnection,
-   428 dbTransaction))
-   429 {
-   430 sqliteCommand.Parameters.AddRange(new[]
-   431 {
-   432 new SQLiteParameter("@name", transferSnapshot.Name),
-   433 new SQLiteParameter("@path", transferSnapshot.Path),
-   434 new SQLiteParameter("@time", transferSnapshot.Time),
-   435 new SQLiteParameter("@dataLength",
-   436 dataMemoryStream.Length),
-   437 new SQLiteParameter("@shotLength",
-   438 bitmapMemoryStream.Length),
-   439 new SQLiteParameter("@hash", transferSnapshot.Hash),
-   440 new SQLiteParameter("@note", transferSnapshot.Note)
-   441 });
-   442  
-   443 var numeric = transferSnapshot.Color;
-   444 switch (numeric)
-   445 {
-   446 case 0:
-   447 sqliteCommand.Parameters.Add(
-   448 new SQLiteParameter("@color", null));
-   449 break;
-   450 default:
-   451 sqliteCommand.Parameters.Add(
-   452 new SQLiteParameter("@color", numeric));
-   453 break;
-   454 }
-   455  
-   456 sqliteCommand.Prepare();
-   457  
-   458 await sqliteCommand.ExecuteNonQueryAsync(cancellationToken);
-   459 }
-   460  
-   461 // Insert the data blobs.
-   462 using (var sqliteCommand =
-   463 new SQLiteCommand(GetLastRowInsertSql, sqliteConnection,
-   464 dbTransaction))
-   465 {
-   466 sqliteCommand.Prepare();
-   467  
-   468 var rowId =
-   469 (long)await sqliteCommand.ExecuteScalarAsync(
-   470 cancellationToken);
-   471  
-   472 using (var sqliteBlob =
-   473 SQLiteBlob.Create(sqliteConnection, "main",
-   474 "Snapshots",
-   475 "Data",
-   476 rowId,
-   477 false))
-   478 {
-   479 var fileMemoryStreamData = dataMemoryStream.ToArray();
-   480  
-   481 sqliteBlob.Write(fileMemoryStreamData,
-   482 fileMemoryStreamData.Length,
-   483 0);
-   484 }
-   485  
-   486 using (var sqliteBlob =
-   487 SQLiteBlob.Create(sqliteConnection, "main",
-   488 "Snapshots",
-   489 "Shot",
-   490 rowId,
-   491 false))
-   492 {
-   493 var bitmapMemoryStreamData =
-   494 bitmapMemoryStream.ToArray();
-   495  
-   496 sqliteBlob.Write(bitmapMemoryStreamData,
-   497 bitmapMemoryStreamData.Length,
-   498 0);
-   499 }
-   500 }
-   501  
-   502 dbTransaction.Commit();
-   503  
-   504 SnapshotCreate?.Invoke(this,
-   505 new SnapshotCreateSuccessEventArgs(transferSnapshot.Name,
-   506 transferSnapshot.Time, transferSnapshot.Path,
-   507 Color.FromArgb(transferSnapshot.Color),
-   508 transferSnapshot.Hash));
-   509 }
-   510 }
-   511 }
-   512 }
-   513 }
-   514 }
-   515 catch (SQLiteException exception)
-   516 {
-   517 dbTransaction.Rollback();
-   518  
-   519 if (exception.ResultCode != SQLiteErrorCode.Constraint)
-   520 {
-   521 SnapshotCreate?.Invoke(this,
-   522 new SnapshotCreateFailureEventArgs(transferSnapshot.Name, transferSnapshot.Path,
-   523 Color.FromArgb(transferSnapshot.Color), exception));
-   524 }
-   525  
-   526 throw;
-   527 }
-   528 catch (Exception exception)
-   529 {
-   530 dbTransaction.Rollback();
-   531  
-   532 SnapshotCreate?.Invoke(this,
-   533 new SnapshotCreateFailureEventArgs(transferSnapshot.Name, transferSnapshot.Path,
-   534 Color.FromArgb(transferSnapshot.Color), exception));
-   535  
-   536 throw;
-   537 }
-   538 }
-   539 }
-   540 }
-   541 finally
-   542 {
-   543 _databaseLock.Release();
-   544 }
-   545 }
-   546  
-   547  
-   548 public async Task<TransferSnapshot> GetCompleteSnapshot(string hash, CancellationToken cancellationToken)
-   549 {
-   550 await _databaseLock.WaitAsync(cancellationToken);
-   551 try
-   552 {
-   553 using (var sqliteConnection =
-   554 new SQLiteConnection(_sqliteConnectionStringBuilder.ConnectionString))
-   555 {
-   556 await sqliteConnection.OpenAsync(cancellationToken);
-   557  
-   558 // Insert the file change.
-   559 using (var sqliteCommand = new SQLiteCommand(GetTransferSnapshotFromHashSql, sqliteConnection))
-   560 {
-   561  
-   562 sqliteCommand.Parameters.AddRange(new[]
-   563 {
-   564 new SQLiteParameter("@hash", hash)
-   565 });
-   566  
-   567 sqliteCommand.Prepare();
-   568  
-   569 using (var sqlDataReader = await sqliteCommand.ExecuteReaderAsync(cancellationToken))
-   570 {
-   571 //var snapshots = new List<Snapshot>();
-   572 while (await sqlDataReader.ReadAsync(cancellationToken))
-   573 {
-   574 var name = (string)sqlDataReader["Name"];
-   575 var path = (string)sqlDataReader["Path"];
-   576 var time = (string)sqlDataReader["Time"];
-   577  
-   578 var color = Color.Empty;
-   579  
-   580 if (!(sqlDataReader["Color"] is DBNull))
-   581 {
-   582 var dbColor = Convert.ToInt32(sqlDataReader["Color"]);
-   583  
-   584 switch (dbColor)
-   585 {
-   586 case 0:
-   587 color = Color.Empty;
-   588 break;
-   589 default:
-   590 color = Color.FromArgb(dbColor);
-   591 break;
-   592 }
-   593 }
-   594  
-   595 var note = string.Empty;
-   596  
-   597 if (!(sqlDataReader["Note"] is DBNull))
-   598 {
-   599 note = (string)sqlDataReader["Note"];
-   600 }
-   601  
-   602 Bitmap shot = null;
-   603  
-   604 if (!(sqlDataReader["Shot"] is DBNull))
-   605 {
-   606 var readStream = sqlDataReader.GetStream(4);
-   607  
-   608 readStream.Position = 0L;
-   609  
-   610 using (var zipStream = new GZipStream(readStream, CompressionMode.Decompress))
-   611 {
-   612 using (var image = Image.FromStream(zipStream))
-   613 {
-   614 shot = new Bitmap(image);
-   615 }
-   616 }
-   617 }
-   618  
-   619 byte[] data = null;
-   620 if (!(sqlDataReader["Data"] is DBNull))
-   621 {
-   622 using (var readStream = sqlDataReader.GetStream(3))
-   623 {
-   624 using (var memoryStream = new MemoryStream())
-   625 {
-   626 readStream.Position = 0L;
-   627  
-   628 await readStream.CopyToAsync(memoryStream);
-   629  
-   630 memoryStream.Position = 0L;
-   631  
-   632 using (var zipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
-   633 {
-   634 // Do not dispose the returned stream and leave it up to callers to dispose.
-   635 var outputStream = new MemoryStream();
-   636  
-   637 await zipStream.CopyToAsync(outputStream);
-   638  
-   639 outputStream.Position = 0L;
-   640  
-   641 data = outputStream.ToArray();
-   642 }
-   643 }
-   644 }
-   645 }
-   646  
-   647 return new TransferSnapshot(name, path, time, hash, color, shot, note, data);
-   648 }
-   649 }
-   650 }
-   651 }
-   652 }
-   653 finally
-   654 {
-   655 _databaseLock.Release();
-   656 }
-   657  
-   658 return null;
-   659 }
378   660  
379 public async Task CreateSnapshotAsync(string name, string path, Color color, CancellationToken cancellationToken) 661 public async Task CreateSnapshotAsync(string name, string path, Color color, CancellationToken cancellationToken)
380 { 662 {
381 await _databaseLock.WaitAsync(cancellationToken); 663 await _databaseLock.WaitAsync(cancellationToken);
382 try 664 try
Line 1093... Line 1375...
1093 { 1375 {
1094 var readStream = sqlDataReader.GetStream(2); 1376 var readStream = sqlDataReader.GetStream(2);
Line 1095... Line 1377...
1095   1377  
Line 1096... Line 1378...
1096 readStream.Position = 0L; 1378 readStream.Position = 0L;
1097   1379  
1098 using (var zipStream = new GZipStream(readStream, CompressionMode.Decompress)) 1380 try
1099 { 1381 {
-   1382 using (var zipStream = new GZipStream(readStream, CompressionMode.Decompress))
-   1383 {
1100 using (var image = Image.FromStream(zipStream)) 1384 using (var image = Image.FromStream(zipStream))
-   1385 {
1101 { 1386 shot = new Bitmap(image);
1102 shot = new Bitmap(image); 1387 }
-   1388 }
-   1389 }
-   1390 catch (Exception exception)
-   1391 {
-   1392 Log.Error(exception, $"Could not retrieve image preview for snapshot {hash}.");
1103 } 1393 return null;
Line 1104... Line 1394...
1104 } 1394 }
1105 } 1395 }
Line 1499... Line 1789...
1499 _databaseLock.Release(); 1789 _databaseLock.Release();
1500 } 1790 }
1501 } 1791 }
Line 1502... Line 1792...
1502   1792  
-   1793 #endregion
-   1794  
1503 #endregion 1795  
1504 } 1796 }
1505 } 1797 }