Compare commits
1 Commits
main
...
2de10db2a0
Author | SHA1 | Date | |
---|---|---|---|
2de10db2a0 |
@ -33,7 +33,7 @@ Accept: application/json
|
||||
|
||||
###
|
||||
|
||||
GET {{host}}/api/v1/assets/{{ownerId}}/archived-tag/
|
||||
GET {{host}}/api/v1/assets/archived-tag/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
|
@ -27,9 +27,9 @@ public class AssetsController(AssetService assetService) : ControllerBase
|
||||
public IActionResult GetRandomPaths(Guid ownerId) =>
|
||||
Ok(_AssetService.GetRandomPaths(ownerId));
|
||||
|
||||
[HttpGet("{ownerId:guid}/archived-tag")]
|
||||
public IActionResult GetArchivedTag(Guid ownerId) =>
|
||||
Content(_AssetService.GetArchivedTag(ownerId), _ContentType);
|
||||
[HttpGet("archived-tag")]
|
||||
public IActionResult GetArchivedTag() =>
|
||||
Content(_AssetService.GetArchivedTag(), _ContentType);
|
||||
|
||||
[HttpGet("{ownerId:guid}/save-random-paths")]
|
||||
public IActionResult SaveRandomPaths(Guid ownerId) =>
|
||||
|
@ -1,4 +1,3 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
@ -19,12 +18,6 @@ public sealed record Identifier(string[] DirectoryNames,
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static string GetDeviceAssetIds(ReadOnlyCollection<Identifier> identifiers) =>
|
||||
$"'{string.Join($"',{Environment.NewLine}'", (from l in identifiers select GetDeviceAssetId(l)).ToArray())}'";
|
||||
|
||||
internal static string GetDeviceAssetId(Identifier identifier) =>
|
||||
$"{identifier.PaddedId}{identifier.Extension}";
|
||||
|
||||
}
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
|
@ -1,5 +1,6 @@
|
||||
using ImmichToSlideshow.Models;
|
||||
using ImmichToSlideshow.Models.Immich;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Npgsql;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Data;
|
||||
@ -11,10 +12,8 @@ namespace ImmichToSlideshow.Services;
|
||||
public class AssetService(ILogger<Program> logger, AppSettings appSettings)
|
||||
{
|
||||
|
||||
#pragma warning disable CS9124
|
||||
private readonly ILogger<Program> _Logger = logger;
|
||||
private readonly AppSettings _AppSettings = appSettings;
|
||||
#pragma warning restore CS9124
|
||||
|
||||
private static int? ExecuteNonQuery(string connectionString, string commandText, ReadOnlyCollection<NpgsqlParameter> npgsqlParameters)
|
||||
{
|
||||
@ -95,27 +94,21 @@ public class AssetService(ILogger<Program> logger, AppSettings appSettings)
|
||||
return results?.AsReadOnly();
|
||||
}
|
||||
|
||||
public Tag[]? GetArchivedTag(AppSettings appSettings, Guid ownerId)
|
||||
public Tag[]? GetArchivedTag(AppSettings appSettings)
|
||||
{
|
||||
Tag[] results;
|
||||
Guid userId = ownerId;
|
||||
string value = appSettings.ArchivedTag;
|
||||
NpgsqlParameter[] npgsqlParameters =
|
||||
[
|
||||
new NpgsqlParameter(nameof(value), value),
|
||||
new NpgsqlParameter(nameof(userId), userId),
|
||||
];
|
||||
string commandText = CommandText.GetArchivedTag();
|
||||
NpgsqlParameter[] npgsqlParameters = [];
|
||||
string commandText = CommandText.GetArchivedTag(appSettings.ArchivedTag);
|
||||
StringBuilder stringBuilder = GetForJsonPath(appSettings.ConnectionString, commandText, npgsqlParameters.AsReadOnly());
|
||||
string json = stringBuilder.ToString();
|
||||
results = JsonSerializer.Deserialize(json, TagCollectionSourceGenerationContext.Default.TagArray);
|
||||
return results;
|
||||
}
|
||||
|
||||
public string? GetArchivedTag(Guid ownerId)
|
||||
public string? GetArchivedTag()
|
||||
{
|
||||
string result;
|
||||
Tag[]? tags = GetArchivedTag(_AppSettings, ownerId);
|
||||
Tag[]? tags = GetArchivedTag(_AppSettings);
|
||||
result = tags is null || tags.Length != 1 ? null : tags[0].Id;
|
||||
return result;
|
||||
}
|
||||
@ -181,6 +174,9 @@ public class AssetService(ILogger<Program> logger, AppSettings appSettings)
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
private static string GetDeviceAssetIds(ReadOnlyCollection<Identifier> identifiers, ReadOnlyCollection<string> assets) =>
|
||||
$"'{string.Join($"',{Environment.NewLine}'", (from l in identifiers where !assets.Contains($"{l.PaddedId}{l.Extension}") select $"{l.PaddedId}{l.Extension}").ToArray())}'";
|
||||
|
||||
public ReadOnlyCollection<int> SetArchiveImmich(Guid ownerId)
|
||||
{
|
||||
ReadOnlyCollection<int>? results;
|
||||
@ -200,18 +196,33 @@ public class AssetService(ILogger<Program> logger, AppSettings appSettings)
|
||||
results = null;
|
||||
else
|
||||
{
|
||||
Tag[]? tags = GetArchivedTag(_AppSettings, ownerId);
|
||||
Tag[]? tags = GetArchivedTag(_AppSettings);
|
||||
if (tags is null || tags.Length != 1)
|
||||
results = null;
|
||||
else
|
||||
results = SetArchiveImmich(logger, appSettings, ownerId, identifiers.AsReadOnly(), tags.AsReadOnly());
|
||||
results = SetArchiveImmich(_AppSettings, ownerId, identifiers, tags);
|
||||
}
|
||||
}
|
||||
}
|
||||
return results?.AsReadOnly();
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<int> SetArchiveImmich(ILogger<Program> logger, AppSettings appSettings, Guid ownerId, ReadOnlyCollection<Identifier> identifiers, ReadOnlyCollection<Tag> tags)
|
||||
private static ReadOnlyCollection<int> SetArchiveImmich(AppSettings appSettings, Guid ownerId, Identifier[] identifiers, Tag[] tags)
|
||||
{
|
||||
ReadOnlyCollection<int>? results;
|
||||
NpgsqlParameter[] npgsqlParameters = [new NpgsqlParameter(nameof(ownerId), ownerId)];
|
||||
string commandText = CommandText.GetAssetActiveImagePreviewNotDuplicate(appSettings.FilterTags);
|
||||
StringBuilder stringBuilder = GetForJsonPath(appSettings.ConnectionString, commandText, npgsqlParameters.AsReadOnly());
|
||||
string json = stringBuilder.ToString();
|
||||
Asset[]? assets = JsonSerializer.Deserialize(json, AssetCollectionSourceGenerationContext.Default.AssetArray);
|
||||
if (assets is null || assets.Length == 0)
|
||||
results = null;
|
||||
else
|
||||
results = SetArchiveImmich(appSettings, ownerId, identifiers, tags, assets);
|
||||
return results?.AsReadOnly();
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<int> SetArchiveImmich(AppSettings appSettings, Guid ownerId, Identifier[]? identifiers, Tag[]? tags, Asset[]? assets)
|
||||
{
|
||||
ReadOnlyCollection<int>? results;
|
||||
string tagsId = tags[0].Id;
|
||||
@ -220,9 +231,9 @@ public class AssetService(ILogger<Program> logger, AppSettings appSettings)
|
||||
new NpgsqlParameter(nameof(tagsId), tagsId),
|
||||
new NpgsqlParameter(nameof(ownerId), ownerId),
|
||||
];
|
||||
string deviceAssetIds = Identifier.GetDeviceAssetIds(identifiers);
|
||||
string[] assetDeviceAssetIds = (from l in assets select l.DeviceAssetId).ToArray();
|
||||
string deviceAssetIds = GetDeviceAssetIds(identifiers.AsReadOnly(), assetDeviceAssetIds.AsReadOnly());
|
||||
string commandText = CommandText.SetAssetArchived(deviceAssetIds);
|
||||
logger.LogDebug(commandText.Replace($"@{nameof(tagsId)}", $"'{tagsId}'").Replace($"@{nameof(ownerId)}", $"'{ownerId}'".ToString()));
|
||||
int? result = ExecuteNonQuery(appSettings.ConnectionString, commandText, npgsqlParameters.AsReadOnly());
|
||||
results = result is null ? null : new([result.Value]);
|
||||
return results?.AsReadOnly();
|
||||
|
@ -103,27 +103,15 @@ internal static class CommandText
|
||||
results.Add(" AND a.\"deviceAssetId\" in ( ");
|
||||
results.Add(deviceAssetIds);
|
||||
results.Add(" ) ");
|
||||
results.Add(" AND a.\"id\" not in ( ");
|
||||
results.Add(" SELECT \"id\" ");
|
||||
results.Add(" FROM assets b ");
|
||||
results.Add(" INNER ");
|
||||
results.Add(" JOIN tag_asset t ");
|
||||
results.Add(" ON b.\"id\" = t.\"assetsId\" ");
|
||||
results.Add(" WHERE t.\"tagsId\" = @tagsId::uuid ");
|
||||
results.Add(" AND b.\"deviceAssetId\" in ( ");
|
||||
results.Add(deviceAssetIds);
|
||||
results.Add(" ) ");
|
||||
results.Add(" ) ");
|
||||
return string.Join(Environment.NewLine, results);
|
||||
} // cSpell:enable
|
||||
|
||||
internal static string GetArchivedTag()
|
||||
internal static string GetArchivedTag(string archivedTag)
|
||||
{ // cSpell:disable
|
||||
List<string> results = new();
|
||||
results.Add(" SELECT json_agg(t) ");
|
||||
results.Add(" FROM tags t ");
|
||||
results.Add($" WHERE t.\"value\" = @value ");
|
||||
results.Add(" AND t.\"userId\" = @userId ");
|
||||
results.Add($" WHERE value = '{archivedTag}' ");
|
||||
return string.Join(Environment.NewLine, results);
|
||||
} // cSpell:enable
|
||||
|
||||
|
Reference in New Issue
Block a user