ShareX/UploadersLib/FileUploaders/OwnCloud.cs

102 lines
3.7 KiB
C#
Raw Normal View History

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
Copyright (C) 2007-2014 ShareX Developers
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 04:29:26 +12:00
using HelpersLib;
2014-07-07 02:56:43 +12:00
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
namespace UploadersLib.FileUploaders
{
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; }
public OwnCloud(string host, string username, string password)
{
Host = host;
Username = username;
Password = password;
}
2014-07-07 04:29:26 +12:00
public string ShareFile(string path)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("format", "json");
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)
string url = URLHelpers.CombineURL(Host, "ocs/v1.php/apps/files_sharing/api/v1/shares");
NameValueCollection headers = CreateAuthenticationHeader(Username, Password);
2014-07-07 04:55:50 +12:00
string response = SendRequest(HttpMethod.GET, url, args, headers);
2014-07-07 04:29:26 +12:00
if (!string.IsNullOrEmpty(response))
{
return ""; // Parse response
}
return null;
}
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 = "/";
}
2014-07-07 04:29:26 +12:00
string url = URLHelpers.CombineURL(Host, "remote.php/webdav", Path);
2014-07-07 02:56:43 +12:00
NameValueCollection headers = CreateAuthenticationHeader(Username, Password);
2014-07-07 04:29:26 +12:00
UploadResult result = UploadData(stream, url, fileName, headers: headers, method: HttpMethod.PUT);
2014-07-07 02:56:43 +12:00
if (result.IsSuccess)
{
2014-07-07 04:29:26 +12:00
result.URL = ShareFile(Path);
2014-07-07 02:56:43 +12:00
}
return result;
}
}
}