70 lines
2.2 KiB
C#
70 lines
2.2 KiB
C#
using SvetoforVKBot.Models.Updates;
|
|
using System;
|
|
using System.Data.SqlClient;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using VkNet;
|
|
|
|
namespace SvetoforVKBot.Models.Commands
|
|
{
|
|
public abstract class Command
|
|
{
|
|
public abstract string Name { get; }
|
|
|
|
public abstract void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db);
|
|
|
|
public abstract void ExecutePL(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db);
|
|
|
|
public bool StartsWith(string command)
|
|
{
|
|
return command.StartsWith(this.Name);
|
|
}
|
|
|
|
private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create();
|
|
public int GetRandomId()
|
|
{
|
|
|
|
var intBytes = new byte[4];
|
|
|
|
Rng.GetBytes(intBytes);
|
|
|
|
return BitConverter.ToInt32(intBytes, 0);
|
|
|
|
}
|
|
|
|
public async Task<string> UploadFile(string serverUrl, string file, string fileExtension)
|
|
{
|
|
// Получение массива байтов из файла
|
|
var data = GetBytesNet(file);
|
|
|
|
// Создание запроса на загрузку файла на сервер
|
|
using (var client = new HttpClient())
|
|
{
|
|
var requestContent = new MultipartFormDataContent();
|
|
var content = new ByteArrayContent(data);
|
|
content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
|
|
requestContent.Add(content, "file", $"file.{fileExtension}");
|
|
|
|
var response = client.PostAsync(serverUrl, requestContent).Result;
|
|
return Encoding.Default.GetString(await response.Content.ReadAsByteArrayAsync());
|
|
}
|
|
}
|
|
private byte[] GetBytesNet(string fileUrl)
|
|
{
|
|
using (var webClient = new WebClient())
|
|
{
|
|
return webClient.DownloadData(fileUrl);
|
|
}
|
|
}
|
|
private byte[] GetBytesLocal(string filePath)
|
|
{
|
|
return File.ReadAllBytes(filePath);
|
|
}
|
|
}
|
|
}
|