Files
VRCX/ScreenshotMetadataDatabase.cs
Teacup 0a0f166a2b feat: Add searching & fullscreen mode to screenshot viewer (#627)
* Optimized search screenshots by metadata

* feat: Screenshot metadata search bar

* fix: Reset search when selecting a file manually

* refactor: Re-do the whole search thing. Add number of results to dialog when searching

* fix: Add check & error for null metadata

* fix: Add sourceFile to error obj on return

* fix: Fix screenshot file dialog not sending path back to JS

* fix: Stop lfs parsing from dying if a value doesn't exist

* fix: Fix and optimize FileStream reading of metadata for searches

* fix: Reset search data and revert to normal when user clears out search box

* refactor: Remove/optimize some old screenshot helper stuff

- Use FileStream in ReadPNGResolution
- Limit the FindChunkIndex search range used when writing metadata
- Remove old ReadPNGDescription, just use filestream version now

* fix: Reset metadata search state if a file is added manually

* feat: Move viewer popover dialog to the fullscreen image viewer

* refactor: Change how parsing errors are handled... again

* refactor: Let the search carousel loop around

* fix: Re-do legacy parsing /wo JObject. Fix legacy instance ids/pos.

Also adds further docs to the legacy parsing for the various formats

* feat: Add persistent metadata cache for search

* Clean up

* fix: Fix viewer dying

sourceFile wasn't being included for vrcx pics

* refactor: Cache the state of files with no metadata

This is so we're not constantly re-processing these files with no metadata on every first search after a restart; These files won't magically gain metadata and this could cause a lot of hitching for someone that had potentially thousands of screenshots before using VRCX.

* Screenshot viewer loading

---------

Co-authored-by: Nekromateion <43814053+Nekromateion@users.noreply.github.com>
Co-authored-by: Natsumi <cmcooper123@hotmail.com>
2023-09-03 09:02:09 +12:00

86 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLite;
namespace VRCX
{
[Table("cache")]
public class MetadataCache
{
[PrimaryKey, AutoIncrement]
[Column("id")]
public int Id { get; set; }
[Column("file_path"), NotNull, Indexed]
public string FilePath { get; set; }
[Column("metadata")]
public string Metadata { get; set; }
[Column("cached_at"), NotNull]
public DateTimeOffset CachedAt { get; set; }
}
// Imagine using SQLite to store json strings in one table lmao
// Couldn't be me... oh wait
internal class ScreenshotMetadataDatabase
{
private SQLiteConnection sqlite;
public ScreenshotMetadataDatabase(string databaseLocation)
{
var options = new SQLiteConnectionString(databaseLocation, true);
sqlite = new SQLiteConnection(options);
sqlite.CreateTable<MetadataCache>();
}
public void AddMetadataCache(string filePath, string metadata)
{
var cache = new MetadataCache()
{
FilePath = filePath,
Metadata = metadata,
CachedAt = DateTimeOffset.Now
};
sqlite.Insert(cache);
}
public void BulkAddMetadataCache(IEnumerable<MetadataCache> cache)
{
sqlite.InsertAll(cache, runInTransaction: true);
}
public int IsFileCached(string filePath)
{
var query = sqlite.Table<MetadataCache>().Where(c => c.FilePath == filePath).Select(c => c.Id);
if (query.Any())
{
return query.First();
}
else
{
return -1;
}
}
public string GetMetadata(string filePath)
{
var query = sqlite.Table<MetadataCache>().Where(c => c.FilePath == filePath).Select(c => c.Metadata);
return query.FirstOrDefault();
}
public string GetMetadataById(int id)
{
var query = sqlite.Table<MetadataCache>().Where(c => c.Id == id).Select(c => c.Metadata);
return query.FirstOrDefault();
}
public void Close()
{
sqlite.Close();
}
}
}