mirror of
https://github.com/JustArchiNET/ArchiSteamFarm.git
synced 2026-01-01 06:00:46 +00:00
Closes #2337
This commit is contained in:
98
ArchiSteamFarm/IPC/Controllers/Api/StorageController.cs
Normal file
98
ArchiSteamFarm/IPC/Controllers/Api/StorageController.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
// _ _ _ ____ _ _____
|
||||
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
|
||||
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
|
||||
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
|
||||
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
|
||||
// |
|
||||
// Copyright 2015-2021 Łukasz "JustArchi" Domeradzki
|
||||
// Contact: JustArchi@JustArchi.net
|
||||
// |
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// |
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
// |
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using ArchiSteamFarm.Core;
|
||||
using ArchiSteamFarm.IPC.Responses;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace ArchiSteamFarm.IPC.Controllers.Api {
|
||||
[Route("Api/Storage")]
|
||||
public sealed class StorageController : ArchiController {
|
||||
/// <summary>
|
||||
/// Deletes entry under specified key from ASF's persistent KeyValue JSON storage.
|
||||
/// </summary>
|
||||
[HttpDelete("{key:required}")]
|
||||
[ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.OK)]
|
||||
public ActionResult<GenericResponse> StorageDelete(string key) {
|
||||
if (string.IsNullOrEmpty(key)) {
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
if (ASF.GlobalDatabase == null) {
|
||||
throw new InvalidOperationException(nameof(ASF.GlobalDatabase));
|
||||
}
|
||||
|
||||
ASF.GlobalDatabase.DeleteFromJsonStorage(key);
|
||||
|
||||
return Ok(new GenericResponse(true));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads entry under specified key from ASF's persistent KeyValue JSON storage.
|
||||
/// </summary>
|
||||
[HttpGet("{key:required}")]
|
||||
[ProducesResponseType(typeof(GenericResponse<JToken>), (int) HttpStatusCode.OK)]
|
||||
public ActionResult<GenericResponse> StorageGet(string key) {
|
||||
if (string.IsNullOrEmpty(key)) {
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
if (ASF.GlobalDatabase == null) {
|
||||
throw new InvalidOperationException(nameof(ASF.GlobalDatabase));
|
||||
}
|
||||
|
||||
JToken? value = ASF.GlobalDatabase.LoadFromJsonStorage(key);
|
||||
|
||||
return Ok(new GenericResponse<JToken>(true, value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves entry under specified key in ASF's persistent KeyValue JSON storage.
|
||||
/// </summary>
|
||||
[Consumes("application/json")]
|
||||
[HttpPost("{key:required}")]
|
||||
[ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.OK)]
|
||||
public ActionResult<GenericResponse> StoragePost(string key, [FromBody] JToken value) {
|
||||
if (string.IsNullOrEmpty(key)) {
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
if (value == null) {
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
if (ASF.GlobalDatabase == null) {
|
||||
throw new InvalidOperationException(nameof(ASF.GlobalDatabase));
|
||||
}
|
||||
|
||||
if (value.Type == JTokenType.Null) {
|
||||
ASF.GlobalDatabase.DeleteFromJsonStorage(key);
|
||||
} else {
|
||||
ASF.GlobalDatabase.SaveToJsonStorage(key, value);
|
||||
}
|
||||
|
||||
return Ok(new GenericResponse(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,7 @@ using ArchiSteamFarm.Steam;
|
||||
using ArchiSteamFarm.Steam.SteamKit2;
|
||||
using JetBrains.Annotations;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace ArchiSteamFarm.Storage {
|
||||
public sealed class GlobalDatabase : SerializableFile {
|
||||
@@ -54,6 +55,9 @@ namespace ArchiSteamFarm.Storage {
|
||||
[JsonProperty(Required = Required.DisallowNull)]
|
||||
internal readonly InMemoryServerListProvider ServerListProvider = new();
|
||||
|
||||
[JsonProperty(Required = Required.DisallowNull)]
|
||||
private readonly ConcurrentDictionary<string, JToken> KeyValueJsonStorage = new();
|
||||
|
||||
[JsonProperty(Required = Required.DisallowNull)]
|
||||
private readonly ConcurrentDictionary<uint, ulong> PackagesAccessTokens = new();
|
||||
|
||||
@@ -164,6 +168,18 @@ namespace ArchiSteamFarm.Storage {
|
||||
return globalDatabase;
|
||||
}
|
||||
|
||||
internal void DeleteFromJsonStorage(string key) {
|
||||
if (string.IsNullOrEmpty(key)) {
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
if (!KeyValueJsonStorage.TryRemove(key, out _)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Utilities.InBackground(Save);
|
||||
}
|
||||
|
||||
internal HashSet<uint> GetPackageIDs(uint appID, IEnumerable<uint> packageIDs) {
|
||||
if (appID == 0) {
|
||||
throw new ArgumentOutOfRangeException(nameof(appID));
|
||||
@@ -186,6 +202,14 @@ namespace ArchiSteamFarm.Storage {
|
||||
return result;
|
||||
}
|
||||
|
||||
internal JToken? LoadFromJsonStorage(string key) {
|
||||
if (string.IsNullOrEmpty(key)) {
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
return KeyValueJsonStorage.TryGetValue(key, out JToken? value) ? value : null;
|
||||
}
|
||||
|
||||
internal async Task OnPICSChangesRestart(uint currentChangeNumber) {
|
||||
if (currentChangeNumber == 0) {
|
||||
throw new ArgumentOutOfRangeException(nameof(currentChangeNumber));
|
||||
@@ -280,6 +304,23 @@ namespace ArchiSteamFarm.Storage {
|
||||
}
|
||||
}
|
||||
|
||||
internal void SaveToJsonStorage(string key, JToken value) {
|
||||
if (string.IsNullOrEmpty(key)) {
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
if (value == null) {
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
if (KeyValueJsonStorage.TryGetValue(key, out JToken? currentValue) && JToken.DeepEquals(currentValue, value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
KeyValueJsonStorage[key] = value;
|
||||
Utilities.InBackground(Save);
|
||||
}
|
||||
|
||||
private async void OnObjectModified(object? sender, EventArgs e) {
|
||||
if (string.IsNullOrEmpty(FilePath)) {
|
||||
return;
|
||||
@@ -291,6 +332,7 @@ namespace ArchiSteamFarm.Storage {
|
||||
// ReSharper disable UnusedMember.Global
|
||||
public bool ShouldSerializeBackingCellID() => BackingCellID != 0;
|
||||
public bool ShouldSerializeBackingLastChangeNumber() => LastChangeNumber != 0;
|
||||
public bool ShouldSerializeKeyValueJsonStorage() => !KeyValueJsonStorage.IsEmpty;
|
||||
public bool ShouldSerializePackagesAccessTokens() => !PackagesAccessTokens.IsEmpty;
|
||||
public bool ShouldSerializePackagesData() => !PackagesData.IsEmpty;
|
||||
public bool ShouldSerializeServerListProvider() => ServerListProvider.ShouldSerializeServerRecords();
|
||||
|
||||
Reference in New Issue
Block a user