网站首页 网站源码
using System.IO;
using System.Threading.Tasks;
using Dpz.Core.Service.ObjectStorage;
using Dpz.Core.Service.ObjectStorage.Services;
using Dpz.Core.Service.RepositoryService;
using Hangfire;
using MongoDB.Bson;
#pragma warning disable CS8621
namespace Dpz.Core.Web.Library.Activator;
[Obsolete]
public class SteamActivator : JobActivator
{
private readonly ISteamGameService _steamGameService;
private readonly IObjectStorageOperation _objectStorageService;
public SteamActivator(
ISteamGameService steamGameService,
IObjectStorageOperation objectStorageService)
{
_steamGameService = steamGameService;
_objectStorageService = objectStorageService;
}
[ProlongExpirationTime]
public async Task UpdateLibraryAsync()
{
_steamGameService.OnLogoDownloadComplete += SteamGameServiceOnOnLogoDownloadComplete;
_steamGameService.OnAchievementIconDownloadComplete += SteamGameServiceOnOnAchievementIconDownloadComplete;
_steamGameService.OnAchievementIconGrayDownloadComplete += SteamGameServiceOnOnAchievementIconGrayDownloadComplete;
await _steamGameService.UpdateGamesAsync();
}
private async Task<string> SteamGameServiceOnOnAchievementIconGrayDownloadComplete(Stream stream,int id,string name)
{
if (stream == null)
return "";
var result = await _objectStorageService.UploadAsync(stream, new[] {"images", "steam","gray_icon",id.ToString()}, $"{ObjectId.GenerateNewId()}.jpg");
return result.AccessUrl;
}
private async Task<string> SteamGameServiceOnOnAchievementIconDownloadComplete(Stream stream,int id,string name)
{
if (stream == null)
return "";
var result = await _objectStorageService.UploadAsync(stream, new[] {"images", "steam","icon",id.ToString()}, $"{ObjectId.GenerateNewId()}.jpg");
return result.AccessUrl;
}
private async Task<string> SteamGameServiceOnOnLogoDownloadComplete(Stream stream, int id)
{
if (stream == null)
return "";
var result = await _objectStorageService.UploadAsync(stream, new[] {"images", "steam","logo"}, $"{id}.jpg");
return result.AccessUrl;
}
}
这段代码定义了一个名为 SteamActivator
的类,它是一个 Hangfire 的作业激活器,主要用于处理与 Steam 游戏相关的图像下载和上传操作。以下是代码的主要功能和结构的详细解释:
SteamActivator
继承自 JobActivator
,这是 Hangfire 中用于创建作业的类。ISteamGameService
: 处理 Steam 游戏相关操作的服务。IObjectStorageOperation
: 处理对象存储操作的服务,用于上传图像。UpdateLibraryAsync
方法中,SteamActivator
订阅了 ISteamGameService
的三个事件:OnLogoDownloadComplete
: 当游戏的 logo 下载完成时触发。OnAchievementIconDownloadComplete
: 当成就图标下载完成时触发。OnAchievementIconGrayDownloadComplete
: 当成就灰色图标下载完成时触发。ISteamGameService
的 UpdateGamesAsync
方法来更新游戏库。这个方法会触发上述事件。每个事件处理方法都接收一个 Stream
(图像数据流)、一个 id
(游戏或成就的标识符)和一个 name
(图像名称)。这些方法的主要功能是将下载的图像上传到对象存储服务,并返回图像的访问 URL。
SteamGameServiceOnOnLogoDownloadComplete:
stream
不为 null
,则调用 UploadAsync
方法将 logo 上传到对象存储,路径为 images/steam/logo/{id}.jpg
。SteamGameServiceOnOnAchievementIconDownloadComplete:
stream
不为 null
,则调用 UploadAsync
方法将图标上传到对象存储,路径为 images/steam/icon/{id}.jpg
。SteamGameServiceOnOnAchievementIconGrayDownloadComplete:
stream
不为 null
,则调用 UploadAsync
方法将灰色图标上传到对象存储,路径为 images/steam/gray_icon/{id}.jpg
。整体而言,SteamActivator
类的功能是通过 ISteamGameService
更新 Steam 游戏库,并在下载游戏相关图像(logo 和成就图标)时,将这些图像上传到对象存储服务。每个图像上传后,返回其访问 URL。这个类的设计使得图像处理和存储操作能够异步进行,提高了应用的响应性和性能。