2014-07-07 02:56:43 +12:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2021-07-29 15:22:51 +12:00
|
|
|
|
Copyright (c) 2007-2021 ShareX Team
|
2014-07-07 02:56:43 +12: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)
|
|
|
|
|
|
2014-07-07 06:45:36 +12:00
|
|
|
|
using Newtonsoft.Json;
|
2015-11-28 15:18:33 +13:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2014-12-11 12:19:28 +13:00
|
|
|
|
using ShareX.HelpersLib;
|
2016-06-28 05:20:55 +12:00
|
|
|
|
using ShareX.UploadersLib.Properties;
|
2014-07-07 02:56:43 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Specialized;
|
2016-06-28 05:20:55 +12:00
|
|
|
|
using System.Drawing;
|
2014-07-07 02:56:43 +12:00
|
|
|
|
using System.IO;
|
2016-05-01 20:09:11 +12:00
|
|
|
|
using System.Windows.Forms;
|
2014-07-07 02:56:43 +12:00
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.UploadersLib.FileUploaders
|
2014-07-07 02:56:43 +12:00
|
|
|
|
{
|
2016-03-23 06:24:41 +13:00
|
|
|
|
public class OwnCloudFileUploaderService : FileUploaderService
|
|
|
|
|
{
|
|
|
|
|
public override FileDestination EnumValue { get; } = FileDestination.OwnCloud;
|
|
|
|
|
|
2016-06-28 05:20:55 +12:00
|
|
|
|
public override Image ServiceImage => Resources.OwnCloud;
|
|
|
|
|
|
2016-03-24 07:47:59 +13:00
|
|
|
|
public override bool CheckConfig(UploadersConfig config)
|
2016-03-23 06:24:41 +13:00
|
|
|
|
{
|
2016-03-24 07:47:59 +13:00
|
|
|
|
return !string.IsNullOrEmpty(config.OwnCloudHost) && !string.IsNullOrEmpty(config.OwnCloudUsername) && !string.IsNullOrEmpty(config.OwnCloudPassword);
|
2016-03-23 06:24:41 +13:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-04 21:53:11 +12:00
|
|
|
|
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
|
2016-03-23 06:24:41 +13:00
|
|
|
|
{
|
2016-03-24 07:47:59 +13:00
|
|
|
|
return new OwnCloud(config.OwnCloudHost, config.OwnCloudUsername, config.OwnCloudPassword)
|
2016-03-23 06:24:41 +13:00
|
|
|
|
{
|
2016-03-24 07:47:59 +13:00
|
|
|
|
Path = config.OwnCloudPath,
|
|
|
|
|
CreateShare = config.OwnCloudCreateShare,
|
|
|
|
|
DirectLink = config.OwnCloudDirectLink,
|
2018-03-25 11:04:56 +13:00
|
|
|
|
PreviewLink = config.OwnCloudUsePreviewLinks,
|
2021-10-27 01:27:57 +13:00
|
|
|
|
AnimationEnabled = config.OwnCloudAnimationFriendlyLinks,
|
2018-09-11 04:55:07 +12:00
|
|
|
|
IsCompatibility81 = config.OwnCloud81Compatibility,
|
2018-09-11 09:14:32 +12:00
|
|
|
|
AutoExpireTime = config.OwnCloudExpiryTime,
|
2018-09-11 04:55:07 +12:00
|
|
|
|
AutoExpire = config.OwnCloudAutoExpire
|
2016-03-23 06:24:41 +13:00
|
|
|
|
};
|
|
|
|
|
}
|
2016-05-01 20:09:11 +12:00
|
|
|
|
|
|
|
|
|
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpOwnCloud;
|
2016-03-23 06:24:41 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-07 02:56:43 +12:00
|
|
|
|
public sealed class OwnCloud : FileUploader
|
|
|
|
|
{
|
|
|
|
|
public string Host { get; set; }
|
|
|
|
|
public string Username { get; set; }
|
|
|
|
|
public string Password { get; set; }
|
|
|
|
|
public string Path { get; set; }
|
2018-09-11 09:14:32 +12:00
|
|
|
|
public int AutoExpireTime { get; set; }
|
2014-07-07 07:51:17 +12:00
|
|
|
|
public bool CreateShare { get; set; }
|
2021-10-27 01:27:57 +13:00
|
|
|
|
public bool AnimationEnabled { get; set; }
|
2014-07-07 09:06:37 +12:00
|
|
|
|
public bool DirectLink { get; set; }
|
2018-03-25 11:04:56 +13:00
|
|
|
|
public bool PreviewLink { get; set; }
|
2015-07-22 09:52:00 +12:00
|
|
|
|
public bool IsCompatibility81 { get; set; }
|
2018-09-11 04:55:07 +12:00
|
|
|
|
public bool AutoExpire { get; set; }
|
2014-07-07 02:56:43 +12:00
|
|
|
|
|
2014-07-07 07:51:17 +12:00
|
|
|
|
public OwnCloud(string host, string username, string password)
|
2014-07-07 02:56:43 +12:00
|
|
|
|
{
|
|
|
|
|
Host = host;
|
|
|
|
|
Username = username;
|
|
|
|
|
Password = password;
|
2014-07-07 04:29:26 +12:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-07 02:56:43 +12:00
|
|
|
|
public override UploadResult Upload(Stream stream, string fileName)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(Host))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("ownCloud Host is empty.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("ownCloud Username or Password is empty.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(Path))
|
|
|
|
|
{
|
|
|
|
|
Path = "/";
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-28 14:12:44 +13:00
|
|
|
|
// Original, unencoded path. Necessary for shared files
|
2014-07-07 06:45:36 +12:00
|
|
|
|
string path = URLHelpers.CombineURL(Path, fileName);
|
2015-11-28 20:57:41 +13:00
|
|
|
|
// Encoded path, necessary when sent in the URL
|
|
|
|
|
string encodedPath = URLHelpers.CombineURL(Path, URLHelpers.URLEncode(fileName));
|
2015-11-28 14:12:44 +13:00
|
|
|
|
|
|
|
|
|
string url = URLHelpers.CombineURL(Host, "remote.php/webdav", encodedPath);
|
2014-08-07 03:55:48 +12:00
|
|
|
|
url = URLHelpers.FixPrefix(url);
|
2017-03-30 10:03:08 +13:00
|
|
|
|
|
2019-03-15 00:06:54 +13:00
|
|
|
|
NameValueCollection headers = RequestHelpers.CreateAuthenticationHeader(Username, Password);
|
2016-12-24 10:34:07 +13:00
|
|
|
|
headers["OCS-APIREQUEST"] = "true";
|
2017-03-30 10:03:08 +13:00
|
|
|
|
|
2019-03-15 00:06:54 +13:00
|
|
|
|
string response = SendRequest(HttpMethod.PUT, url, stream, RequestHelpers.GetMimeType(fileName), null, headers);
|
2014-07-07 07:51:17 +12:00
|
|
|
|
|
2016-03-25 05:21:17 +13:00
|
|
|
|
UploadResult result = new UploadResult(response);
|
2014-08-11 22:40:14 +12:00
|
|
|
|
|
2016-03-25 05:21:17 +13:00
|
|
|
|
if (!IsError)
|
|
|
|
|
{
|
|
|
|
|
if (CreateShare)
|
2014-07-07 07:51:17 +12:00
|
|
|
|
{
|
2016-03-25 05:21:17 +13:00
|
|
|
|
AllowReportProgress = false;
|
|
|
|
|
result.URL = ShareFile(path);
|
2014-07-07 07:51:17 +12:00
|
|
|
|
}
|
2016-03-25 05:21:17 +13:00
|
|
|
|
else
|
2014-08-11 22:40:14 +12:00
|
|
|
|
{
|
2016-03-25 05:21:17 +13:00
|
|
|
|
result.IsURLExpected = false;
|
2014-08-11 22:40:14 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-25 05:21:17 +13:00
|
|
|
|
|
|
|
|
|
return result;
|
2014-07-07 02:56:43 +12:00
|
|
|
|
}
|
2014-07-07 06:45:36 +12:00
|
|
|
|
|
2018-09-11 04:55:07 +12:00
|
|
|
|
// https://doc.owncloud.org/server/10.0/developer_manual/core/ocs-share-api.html#create-a-new-share
|
2014-07-07 06:45:36 +12:00
|
|
|
|
public string ShareFile(string path)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
|
|
|
|
args.Add("path", path); // path to the file/folder which should be shared
|
|
|
|
|
args.Add("shareType", "3"); // ‘0’ = user; ‘1’ = group; ‘3’ = public link
|
|
|
|
|
// args.Add("shareWith", ""); // user / group id with which the file should be shared
|
|
|
|
|
// args.Add("publicUpload", "false"); // allow public upload to a public shared folder (true/false)
|
|
|
|
|
// args.Add("password", ""); // password to protect public link Share with
|
|
|
|
|
args.Add("permissions", "1"); // 1 = read; 2 = update; 4 = create; 8 = delete; 16 = share; 31 = all (default: 31, for public shares: 1)
|
|
|
|
|
|
2018-09-11 04:55:07 +12:00
|
|
|
|
if (AutoExpire)
|
|
|
|
|
{
|
2018-09-11 09:14:32 +12:00
|
|
|
|
if (AutoExpireTime == 0)
|
2018-09-11 04:55:07 +12:00
|
|
|
|
{
|
2018-09-11 09:14:32 +12:00
|
|
|
|
throw new Exception("ownCloud Auto Epxire Time is not valid.");
|
2018-09-11 04:55:07 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-09-11 09:14:32 +12:00
|
|
|
|
DateTime expireTime = DateTime.UtcNow.AddDays(AutoExpireTime);
|
2018-09-11 04:55:07 +12:00
|
|
|
|
args.Add("expireDate", $"{expireTime.Year}-{expireTime.Month}-{expireTime.Day}");
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("ownCloud Auto Expire time is invalid");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-07 06:45:36 +12:00
|
|
|
|
string url = URLHelpers.CombineURL(Host, "ocs/v1.php/apps/files_sharing/api/v1/shares?format=json");
|
2014-12-15 23:15:49 +13:00
|
|
|
|
url = URLHelpers.FixPrefix(url);
|
2017-03-30 10:03:08 +13:00
|
|
|
|
|
2019-03-15 00:06:54 +13:00
|
|
|
|
NameValueCollection headers = RequestHelpers.CreateAuthenticationHeader(Username, Password);
|
2016-12-30 13:53:53 +13:00
|
|
|
|
headers["OCS-APIREQUEST"] = "true";
|
2017-03-30 10:03:08 +13:00
|
|
|
|
|
2016-12-27 05:30:17 +13:00
|
|
|
|
string response = SendRequestMultiPart(url, args, headers);
|
2014-07-07 06:45:36 +12:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(response))
|
|
|
|
|
{
|
|
|
|
|
OwnCloudShareResponse result = JsonConvert.DeserializeObject<OwnCloudShareResponse>(response);
|
|
|
|
|
|
|
|
|
|
if (result != null && result.ocs != null && result.ocs.meta != null)
|
|
|
|
|
{
|
|
|
|
|
if (result.ocs.data != null && result.ocs.meta.statuscode == 100)
|
|
|
|
|
{
|
2015-11-28 15:18:33 +13:00
|
|
|
|
OwnCloudShareResponseData data = ((JObject)result.ocs.data).ToObject<OwnCloudShareResponseData>();
|
|
|
|
|
string link = data.url;
|
2019-01-12 03:56:00 +13:00
|
|
|
|
if (PreviewLink && Helpers.IsImageFile(path))
|
2018-01-29 21:20:18 +13:00
|
|
|
|
{
|
2019-01-09 11:35:34 +13:00
|
|
|
|
link += "/preview";
|
2018-03-25 11:04:56 +13:00
|
|
|
|
}
|
|
|
|
|
else if (DirectLink)
|
|
|
|
|
{
|
2021-10-27 01:27:57 +13:00
|
|
|
|
link += (IsCompatibility81 ? "/" : "&") + "download" + (AnimationEnabled ? "/" + System.IO.Path.GetFileName(path) : "");
|
2018-01-29 21:20:18 +13:00
|
|
|
|
}
|
2014-07-07 09:06:37 +12:00
|
|
|
|
return link;
|
2014-07-07 06:45:36 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(string.Format("Status: {0}\r\nStatus code: {1}\r\nMessage: {2}", result.ocs.meta.status, result.ocs.meta.statuscode, result.ocs.meta.message));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class OwnCloudShareResponse
|
|
|
|
|
{
|
|
|
|
|
public OwnCloudShareResponseOcs ocs { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class OwnCloudShareResponseOcs
|
|
|
|
|
{
|
|
|
|
|
public OwnCloudShareResponseMeta meta { get; set; }
|
2015-11-28 15:18:33 +13:00
|
|
|
|
public object data { get; set; }
|
2014-07-07 06:45:36 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class OwnCloudShareResponseMeta
|
|
|
|
|
{
|
|
|
|
|
public string status { get; set; }
|
|
|
|
|
public int statuscode { get; set; }
|
|
|
|
|
public string message { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class OwnCloudShareResponseData
|
|
|
|
|
{
|
|
|
|
|
public int id { get; set; }
|
|
|
|
|
public string url { get; set; }
|
|
|
|
|
public string token { get; set; }
|
|
|
|
|
}
|
2014-07-07 02:56:43 +12:00
|
|
|
|
}
|
2019-01-09 11:24:54 +13:00
|
|
|
|
}
|