118 lines
4.1 KiB
C#
Executable File
118 lines
4.1 KiB
C#
Executable File
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;
|
|
using System.Threading.Tasks;
|
|
using JacobTechEncryption;
|
|
using Luski.net.Interfaces;
|
|
|
|
namespace Luski.net.Structures;
|
|
|
|
public class File : IncomingHTTP
|
|
{
|
|
public MainServer Server { get; internal set; }
|
|
[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)
|
|
{
|
|
//TODO make better
|
|
|
|
//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);
|
|
/*
|
|
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)
|
|
{
|
|
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(
|
|
"SocketMessage/UploadFile",
|
|
NPath,
|
|
FileContext.Default.File,
|
|
CancellationToken,
|
|
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;
|
|
return sf.Id;
|
|
}
|
|
|
|
internal void decrypt()
|
|
{
|
|
if (Name is not null) Name = Encoding.UTF8.GetString(Encryption.RSA.Decrypt(Convert.FromBase64String(Name), key));
|
|
}
|
|
}
|
|
|
|
[JsonSerializable(typeof(File))]
|
|
internal partial class FileContext : JsonSerializerContext
|
|
{
|
|
|
|
}
|