Skip to content

Commit

Permalink
get this compiling at least
Browse files Browse the repository at this point in the history
  • Loading branch information
f2calv committed Jul 31, 2024
1 parent 1aac68e commit fec1d7d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 16 deletions.
6 changes: 3 additions & 3 deletions CasCap.GooglePhotosCli.sln
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30128.36
# Visual Studio Version 17
VisualStudioVersion = 17.11.35118.90
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CasCap.GooglePhotosCli", "src\CasCap.GooglePhotosCli\CasCap.GooglePhotosCli.csproj", "{E006EF76-4F01-4079-89C0-30DAEDC92C4F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CasCap.GooglePhotosCli.Tests", "src\CasCap.GooglePhotosCli.Tests\CasCap.GooglePhotosCli.Tests.csproj", "{34AE3CF3-B349-4DA4-984F-96CF09F07A36}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CasCap.GooglePhotosCli.Tests", "src\CasCap.GooglePhotosCli.Tests\CasCap.GooglePhotosCli.Tests.csproj", "{34AE3CF3-B349-4DA4-984F-96CF09F07A36}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CasCap.Common.Testing" Version="3.0.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="CasCap.Common.Testing" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
9 changes: 5 additions & 4 deletions src/CasCap.GooglePhotosCli/CasCap.GooglePhotosCli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
<PackageReference Include="ShellProgressBar" Version="5.2.0" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.1.1" />
<PackageReference Include="McMaster.Extensions.Hosting.CommandLine" Version="4.1.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />
<PackageReference Include="CasCap.Common.Caching" Version="3.0.5" />
<PackageReference Include="CasCap.Common.Serialisation.Json" Version="3.0.5" />
<PackageReference Include="CasCap.Apis.GooglePhotos" Version="3.0.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
<PackageReference Include="CasCap.Common.Caching" Version="3.1.0" />
<PackageReference Include="CasCap.Common.Serialisation.Json" Version="3.1.0" />
<PackageReference Include="CasCap.Apis.GooglePhotos" Version="3.0.1" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
</ItemGroup>

</Project>
14 changes: 10 additions & 4 deletions src/CasCap.GooglePhotosCli/Commands/Base/CommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ protected async Task ReadConfig()

protected async Task<bool> SyncMediaItems()
{
allMediaItems = await _diskCacheSvc.GetAsync($"{nameof(allMediaItems)}.json", () => _googlePhotosSvc.GetMediaItemsAsync());
//allMediaItems = await _diskCacheSvc.GetAsync<List<MediaItem>>($"{nameof(allMediaItems)}.json", () => _googlePhotosSvc.GetMediaItemsAsync());
//TODO: revert to use caching line above here!
allMediaItems = await _googlePhotosSvc.GetMediaItemsAsync().ToListAsync();
_config.latestMediaItemCreation = allMediaItems.Max(p => p.mediaMetadata.creationTime);

//check for duplicate MediaItemIds - it shouldn't be possible to have but somehow I had them...
Expand Down Expand Up @@ -288,7 +290,10 @@ protected async Task<bool> SyncAlbums()
() => _googlePhotosSvc.GetAlbumAsync(a.id));
//_console.WriteLine($"\t{alb.mediaItemsCount}");

var mediaItemsA = await _diskCacheSvc.GetAsync($"album_mediaItems_{a.id}.json", () => _googlePhotosSvc.GetMediaItemsByAlbumAsync(a.id).ToListAsync());
//var mediaItemsA = await _diskCacheSvc.GetAsync($"album_mediaItems_{a.id}.json", () => _googlePhotosSvc.GetMediaItemsByAlbumAsync(a.id).ToListAsync());
//TODO: revert to use caching line above here!
var mediaItemsA = await _googlePhotosSvc.GetMediaItemsByAlbumAsync(a.id).ToListAsync();

//todo: we probably need a dictionary check here also... as you never know with this API!
var d = new Dictionary<string, MediaItem>();
var ids = mediaItemsA.Select(p => p.id).ToList();
Expand Down Expand Up @@ -347,8 +352,9 @@ protected async Task<bool> SyncMediaItemsByCategory()
{
//_console.WriteLine();
//_console.Write(category);
var mis = await _diskCacheSvc.GetAsync($"mediaItems_{category}.json",
() => _googlePhotosSvc.GetMediaItemsByCategoryAsync(category));
//var mis = await _diskCacheSvc.GetAsync($"mediaItems_{category}.json", () => _googlePhotosSvc.GetMediaItemsByCategoryAsync(category));
//TODO: revert to use caching line above here!
var mis = await _googlePhotosSvc.GetMediaItemsByCategoryAsync(category).ToListAsync();
//_console.WriteLine($"\t{mis.Count}");

var d = new Dictionary<string, MediaItem>();
Expand Down
72 changes: 72 additions & 0 deletions src/CasCap.GooglePhotosCli/Extensions/AsyncDuplicateLock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace CasCap.Common.Extensions;

/// <summary>
/// Asynchronous locking based on a key
/// https://www.hanselman.com/blog/EyesWideOpenCorrectCachingIsAlwaysHard.aspx
/// https://stackoverflow.com/questions/31138179/asynchronous-locking-based-on-a-key
/// Note: Not an extension class, stored under this namespace temporarily.
/// </summary>
public sealed class AsyncDuplicateLock
{
sealed class RefCounted<T>
{
public RefCounted(T value)
{
RefCount = 1;
Value = value;
}

public int RefCount { get; set; }
public T Value { get; private set; }
}

static readonly Dictionary<object, RefCounted<SemaphoreSlim>> SemaphoreSlims = new();

static SemaphoreSlim GetOrCreate(object key)
{
RefCounted<SemaphoreSlim>? item;

Check warning on line 27 in src/CasCap.GooglePhotosCli/Extensions/AsyncDuplicateLock.cs

View workflow job for this annotation

GitHub Actions / ci / dotnet-publish-nuget

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 27 in src/CasCap.GooglePhotosCli/Extensions/AsyncDuplicateLock.cs

View workflow job for this annotation

GitHub Actions / ci / dotnet-publish-nuget

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
lock (SemaphoreSlims)
{
if (SemaphoreSlims.TryGetValue(key, out item))
++item.RefCount;
else
{
item = new RefCounted<SemaphoreSlim>(new SemaphoreSlim(1, 1));
SemaphoreSlims[key] = item;
}
}
return item.Value;
}

public static IDisposable Lock(object key)
{
GetOrCreate(key).Wait();
return new Releaser { Key = key };
}

public static async Task<IDisposable> LockAsync(object key)
{
await GetOrCreate(key).WaitAsync().ConfigureAwait(false);
return new Releaser { Key = key };
}

sealed class Releaser : IDisposable
{
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
public object Key { get; set; }
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.

public void Dispose()
{
RefCounted<SemaphoreSlim> item;
lock (SemaphoreSlims)
{
item = SemaphoreSlims[Key];
--item.RefCount;
if (item.RefCount == 0)
SemaphoreSlims.Remove(Key);
}
item.Value.Release();
}
}
}
1 change: 0 additions & 1 deletion src/CasCap.GooglePhotosCli/Services/DiskCacheService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CasCap.Common.Extensions;
using CasCap.Logic;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
namespace CasCap.Services;
Expand Down

0 comments on commit fec1d7d

Please sign in to comment.