2017-02-28 21:34:08 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2023-01-10 09:31:02 +13:00
|
|
|
|
Copyright (c) 2007-2023 ShareX Team
|
2017-02-28 21:34:08 +13:00
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endregion License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
using Newtonsoft.Json;
|
2020-12-26 22:41:31 +13:00
|
|
|
|
using ShareX.HelpersLib;
|
2017-02-27 05:17:02 +13:00
|
|
|
|
using ShareX.UploadersLib.Properties;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
2017-02-27 12:45:07 +13:00
|
|
|
|
using System.Linq;
|
2017-02-27 05:17:02 +13:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace ShareX.UploadersLib.FileUploaders
|
|
|
|
|
{
|
|
|
|
|
public class PlikFileUploaderService : FileUploaderService
|
|
|
|
|
{
|
|
|
|
|
public override FileDestination EnumValue { get; } = FileDestination.Plik;
|
|
|
|
|
|
|
|
|
|
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
|
|
|
|
|
{
|
2021-06-10 10:14:01 +12:00
|
|
|
|
return new Plik(config.PlikSettings);
|
2017-02-27 05:17:02 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool CheckConfig(UploadersConfig config)
|
|
|
|
|
{
|
2017-02-27 09:44:47 +13:00
|
|
|
|
return !string.IsNullOrEmpty(config.PlikSettings.URL) && !string.IsNullOrEmpty(config.PlikSettings.APIKey);
|
2017-02-27 05:17:02 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpPlik;
|
|
|
|
|
|
|
|
|
|
public override Icon ServiceIcon => Resources.Plik;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed class Plik : FileUploader
|
|
|
|
|
{
|
2021-06-10 10:14:01 +12:00
|
|
|
|
public PlikSettings Settings { get; private set; }
|
2017-02-27 05:17:02 +13:00
|
|
|
|
|
2021-06-10 10:14:01 +12:00
|
|
|
|
public Plik(PlikSettings settings)
|
2017-02-27 05:17:02 +13:00
|
|
|
|
{
|
2021-06-10 10:14:01 +12:00
|
|
|
|
Settings = settings;
|
2017-02-27 05:17:02 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override UploadResult Upload(Stream stream, string fileName)
|
|
|
|
|
{
|
2017-02-27 08:27:40 +13:00
|
|
|
|
if (string.IsNullOrEmpty(Settings.URL))
|
2017-02-27 05:17:02 +13:00
|
|
|
|
{
|
|
|
|
|
throw new Exception("Plik Host is empty.");
|
|
|
|
|
}
|
|
|
|
|
NameValueCollection requestHeaders = new NameValueCollection();
|
2017-02-27 08:27:40 +13:00
|
|
|
|
requestHeaders["X-PlikToken"] = Settings.APIKey;
|
2017-02-27 05:17:02 +13:00
|
|
|
|
UploadMetadataRequest metaDataReq = new UploadMetadataRequest();
|
|
|
|
|
metaDataReq.Files = new UploadMetadataRequestFile0();
|
|
|
|
|
metaDataReq.Files.File0 = new UploadMetadataRequestFile();
|
|
|
|
|
metaDataReq.Files.File0.FileName = fileName;
|
2019-03-15 00:06:54 +13:00
|
|
|
|
metaDataReq.Files.File0.FileType = RequestHelpers.GetMimeType(fileName);
|
2017-02-27 05:17:02 +13:00
|
|
|
|
metaDataReq.Files.File0.FileSize = Convert.ToInt32(stream.Length);
|
2017-02-27 08:27:40 +13:00
|
|
|
|
metaDataReq.Removable = Settings.Removable;
|
|
|
|
|
metaDataReq.OneShot = Settings.OneShot;
|
2017-06-11 07:42:54 +12:00
|
|
|
|
if (Settings.TTLUnit != 3) // everything except the expire time -1
|
|
|
|
|
{
|
|
|
|
|
metaDataReq.Ttl = Convert.ToInt32(GetMultiplyIndex(2, Settings.TTLUnit) * Settings.TTL * 60);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
metaDataReq.Ttl = -1;
|
|
|
|
|
}
|
2017-02-27 09:44:47 +13:00
|
|
|
|
if (Settings.HasComment)
|
2017-02-27 05:17:02 +13:00
|
|
|
|
{
|
2017-02-27 08:27:40 +13:00
|
|
|
|
metaDataReq.Comment = Settings.Comment;
|
2017-02-27 05:17:02 +13:00
|
|
|
|
}
|
2017-02-27 09:44:47 +13:00
|
|
|
|
if (Settings.IsSecured)
|
2017-02-27 05:17:02 +13:00
|
|
|
|
{
|
2017-02-27 08:27:40 +13:00
|
|
|
|
metaDataReq.Login = Settings.Login;
|
|
|
|
|
metaDataReq.Password = Settings.Password;
|
2017-02-27 05:17:02 +13:00
|
|
|
|
}
|
2017-02-27 08:27:40 +13:00
|
|
|
|
string metaDataResp = SendRequest(HttpMethod.POST, Settings.URL + "/upload", JsonConvert.SerializeObject(metaDataReq), headers: requestHeaders);
|
2017-02-27 05:17:02 +13:00
|
|
|
|
UploadMetadataResponse metaData = JsonConvert.DeserializeObject<UploadMetadataResponse>(metaDataResp);
|
|
|
|
|
requestHeaders["x-uploadtoken"] = metaData.uploadToken;
|
2020-09-23 22:24:07 +12:00
|
|
|
|
string url = $"{Settings.URL}/file/{metaData.id}/{metaData.files.First().Value.id}/{fileName}";
|
2017-02-27 05:17:02 +13:00
|
|
|
|
UploadResult FileDatReq = SendRequestFile(url, stream, fileName, "file", headers: requestHeaders);
|
|
|
|
|
|
|
|
|
|
return ConvertResult(metaData, FileDatReq);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private UploadResult ConvertResult(UploadMetadataResponse metaData, UploadResult fileDataReq)
|
|
|
|
|
{
|
|
|
|
|
UploadResult result = new UploadResult(fileDataReq.Response);
|
2021-06-11 06:44:40 +12:00
|
|
|
|
//UploadMetadataResponse fileData = JsonConvert.DeserializeObject<UploadMetadataResponse>(fileDataReq.Response);
|
2017-02-27 12:45:07 +13:00
|
|
|
|
UploadMetadataResponseFile actFile = metaData.files.First().Value;
|
2020-12-26 00:21:55 +13:00
|
|
|
|
result.URL = $"{Settings.URL}/file/{metaData.id}/{actFile.id}/{URLHelpers.URLEncode(actFile.fileName)}";
|
2017-02-27 05:17:02 +13:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-11 07:42:54 +12:00
|
|
|
|
internal static void CalculateTTLValue(NumericUpDown ttlElement, int newUnit, int oldUnit)
|
|
|
|
|
{
|
|
|
|
|
if (newUnit != 3)
|
|
|
|
|
{
|
|
|
|
|
if (ttlElement.Value == -1)
|
|
|
|
|
{
|
|
|
|
|
ttlElement.Value = 1;
|
|
|
|
|
}
|
2021-06-10 10:14:01 +12:00
|
|
|
|
ttlElement.Value *= GetMultiplyIndex(newUnit, oldUnit);
|
2017-06-11 07:42:54 +12:00
|
|
|
|
ttlElement.ReadOnly = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ttlElement.Value = -1;
|
|
|
|
|
ttlElement.ReadOnly = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-27 05:17:02 +13:00
|
|
|
|
internal static decimal GetMultiplyIndex(int newUnit, int oldUnit)
|
|
|
|
|
{
|
|
|
|
|
decimal multiplyValue = 1m;
|
|
|
|
|
switch (newUnit)
|
|
|
|
|
{
|
|
|
|
|
case 0: // days
|
|
|
|
|
switch (oldUnit)
|
|
|
|
|
{
|
|
|
|
|
case 1: // hours
|
|
|
|
|
multiplyValue = 1m / 24m;
|
|
|
|
|
break;
|
|
|
|
|
case 2: // minutes
|
|
|
|
|
multiplyValue = 1m / 24m / 60m;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 1: // hours
|
|
|
|
|
switch (oldUnit)
|
|
|
|
|
{
|
|
|
|
|
case 0: // days
|
|
|
|
|
multiplyValue = 24m;
|
|
|
|
|
break;
|
|
|
|
|
case 2: // minutes
|
|
|
|
|
multiplyValue = 1m / 60m;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2: // minutes
|
|
|
|
|
switch (oldUnit)
|
|
|
|
|
{
|
|
|
|
|
case 0: // days
|
|
|
|
|
multiplyValue = 60m * 24m;
|
|
|
|
|
break;
|
|
|
|
|
case 1: // hours
|
|
|
|
|
multiplyValue = 60m;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return multiplyValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class UploadMetadataRequestFile
|
|
|
|
|
{
|
|
|
|
|
[JsonProperty("fileName")]
|
|
|
|
|
public string FileName { get; set; }
|
|
|
|
|
[JsonProperty("fileType")]
|
|
|
|
|
public string FileType { get; set; }
|
|
|
|
|
[JsonProperty("fileSize")]
|
|
|
|
|
public int FileSize { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class UploadMetadataRequestFile0
|
|
|
|
|
{
|
|
|
|
|
[JsonProperty("0")]
|
|
|
|
|
public UploadMetadataRequestFile File0 { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class UploadMetadataRequest
|
|
|
|
|
{
|
|
|
|
|
[JsonProperty("ttl")]
|
|
|
|
|
public int Ttl { get; set; }
|
|
|
|
|
[JsonProperty("removable")]
|
|
|
|
|
public bool Removable { get; set; }
|
|
|
|
|
[JsonProperty("oneShot")]
|
|
|
|
|
public bool OneShot { get; set; }
|
|
|
|
|
[JsonProperty("comments")]
|
|
|
|
|
public string Comment { get; set; }
|
|
|
|
|
[JsonProperty("login")]
|
|
|
|
|
public string Login { get; set; }
|
|
|
|
|
[JsonProperty("password")]
|
|
|
|
|
public string Password { get; set; }
|
|
|
|
|
[JsonProperty("files")]
|
|
|
|
|
public UploadMetadataRequestFile0 Files { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class UploadMetadataResponseFile
|
|
|
|
|
{
|
|
|
|
|
public string id { get; set; }
|
|
|
|
|
public string fileName { get; set; }
|
|
|
|
|
public string fileMd5 { get; set; }
|
|
|
|
|
public string status { get; set; }
|
|
|
|
|
public string fileType { get; set; }
|
|
|
|
|
public int fileUploadDate { get; set; }
|
|
|
|
|
public int fileSize { get; set; }
|
|
|
|
|
public string reference { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class UploadMetadataResponse
|
|
|
|
|
{
|
|
|
|
|
public string id { get; set; }
|
|
|
|
|
public int uploadDate { get; set; }
|
|
|
|
|
public int ttl { get; set; }
|
|
|
|
|
public string shortUrl { get; set; }
|
|
|
|
|
public string downloadDomain { get; set; }
|
|
|
|
|
public string comments { get; set; }
|
|
|
|
|
public Dictionary<string, UploadMetadataResponseFile> files { get; set; }
|
|
|
|
|
public string uploadToken { get; set; }
|
|
|
|
|
public bool admin { get; set; }
|
|
|
|
|
public bool stream { get; set; }
|
|
|
|
|
public bool oneShot { get; set; }
|
|
|
|
|
public bool removable { get; set; }
|
|
|
|
|
public bool protectedByPassword { get; set; }
|
|
|
|
|
public bool protectedByYubikey { get; set; }
|
|
|
|
|
}
|
2020-12-26 22:41:31 +13:00
|
|
|
|
}
|