Luski.Net/Luski.net/JsonTypes/File.cs

108 lines
3.7 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.Tasks;
namespace Luski.net.JsonTypes;
public class File : IncomingHTTP
{
[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 void DownloadBytes(string Loc, long key)
{
//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);
string Key = (key == 0 ? Encryption.MyPublicKey : Encryption.File.Channels.GetKey(key))!;
Encryption.AES.Decrypt(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;
}
internal async Task<long> Upload(string keyy)
{
if (Name != null) Name = Convert.ToBase64String(Encryption.Encrypt(Name, keyy));
Debug.WriteLine("uploading");
string NPath = Encryption.AES.Encrypt(loc, keyy);
File sf = await Server.SendServer(
"SocketMessage/UploadFile",
NPath,
FileContext.Default.File,
new KeyValuePair<string, string?>("name", Name));
try { System.IO.File.Delete(NPath); } catch { }
Debug.WriteLine("done uploading");
if (sf.Error is not null) throw new Exception(sf.ErrorMessage);
return sf.Id;
}
internal void decrypt()
{
if (Name is not null) Name = Encryption.Encoder.GetString(Encryption.Decrypt(Convert.FromBase64String(Name), key));
}
}
[JsonSerializable(typeof(File))]
internal partial class FileContext : JsonSerializerContext
{
}