Luski.Net/Luski.net/Structures/File.cs

119 lines
4.1 KiB
C#
Raw Normal View History

2023-01-01 22:50:39 -05:00
using Luski.net.Enums;
using Luski.net.JsonTypes.BaseTypes;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
2023-01-01 22:50:39 -05:00
using System.Threading.Tasks;
using JacobTechEncryption;
using Luski.net.Interfaces;
2024-03-20 23:18:34 -04:00
using Luski.Shared.PublicServers.V1.ServerToClient.HTTP;
2023-01-01 22:50:39 -05:00
namespace Luski.net.Structures;
2023-01-01 22:50:39 -05:00
2024-03-20 23:18:34 -04:00
public class File : STC
2023-01-01 22:50:39 -05:00
{
public MainServer Server { get; internal set; }
2023-01-01 22:50:39 -05:00
[JsonInclude]
[JsonPropertyName("name")]
public string Name { get; internal set; } = default!;
[JsonInclude]
[JsonPropertyName("size")]
public ulong Size { get; internal set; } = default!;
[JsonInclude]
[JsonPropertyName("id")]
public long Id { get; internal set; } = default!;
[JsonIgnore]
internal string? key { get; set; } = default!;
[JsonIgnore]
internal string loc { get; set; } = default!;
public async Task DownloadBytes(string Loc, long key, CancellationToken CancellationToken)
2023-01-01 22:50:39 -05:00
{
//TODO make better
2023-01-01 22:50:39 -05:00
//using HttpClient web = new();
//web.DefaultRequestHeaders.Add("token", Server.Token);
//web.DefaultRequestHeaders.Add("id", id.ToString());
//IncomingHTTP? request = JsonSerializer.Deserialize(web.GetAsync($"https://{Server.Domain}/{Server.API_Ver}/SocketMessage/GetFile").Result.Content.ReadAsStringAsync().Result, IncomingHTTPContext.Default.IncomingHTTP);
string path = Path.GetTempFileName();
await Server.GetFromServer($"SocketMessage/GetFile/{Id}", path, CancellationToken);
string Key = (key == 0 ? Server.EncryptionHandler.MyPublicKey : "") ;// ClientEncryption.File.Channels.GetKey(key))!;
Encryption.AES.DecryptToFile(System.IO.File.ReadAllBytes(path), Key, Loc);
2023-01-01 22:50:39 -05:00
/*
if (request is not null && request.Error is not null)
{
switch (request.Error)
{
case ErrorCode.InvalidToken:
throw new Exception("Your current token is no longer valid");
case ErrorCode.ServerError:
throw new Exception("Error from server: " + request.ErrorMessage);
case ErrorCode.Forbidden:
throw new Exception("Your request was denied by the server");
default:
MemoryStream? ms = new();
JsonSerializer.Serialize(new Utf8JsonWriter(ms),
request,
IncomingHTTPContext.Default.IncomingHTTP);
throw new Exception(Encoding.UTF8.GetString(ms.ToArray()));
}
}
if (request?.data is not null)
{
foreach (string raw in request.data)
{
Encryption.AES.Decrypt(Convert.FromBase64String(raw), Encryption.File.Channels.GetKey(key), Loc);
}
}*/
}
public void SetFile(string path)
{
FileInfo fi = new(path);
Name = fi.Name;
Size = (ulong)fi.Length;
loc = path;
}
private bool Uploaded = false;
internal async Task<long> Upload(string keyy, CancellationToken CancellationToken)
2023-01-01 22:50:39 -05:00
{
if (Uploaded) return Id;
if (Name != null) Name = Convert.ToBase64String(Encryption.RSA.Encrypt(Encoding.UTF8.GetBytes(Name), keyy));
string NPath = Encryption.AES.EncryptFile(loc, keyy);
File sf = await Server.SendServer(
2023-01-01 22:50:39 -05:00
"SocketMessage/UploadFile",
NPath,
FileContext.Default.File,
CancellationToken,
2023-01-01 22:50:39 -05:00
new KeyValuePair<string, string?>("name", Name));
try { System.IO.File.Delete(NPath); } catch { }
if (sf.Error is not null) throw new Exception(sf.ErrorMessage);
Id = sf.Id;
Uploaded = true;
2023-01-01 22:50:39 -05:00
return sf.Id;
}
internal void decrypt()
{
if (Name is not null) Name = Encoding.UTF8.GetString(Encryption.RSA.Decrypt(Convert.FromBase64String(Name), key));
2023-01-01 22:50:39 -05:00
}
}
[JsonSerializable(typeof(File))]
internal partial class FileContext : JsonSerializerContext
{
}