ShareX/ShareX.UploadersLib/FileUploaders/Box.cs

288 lines
9.3 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2013-11-03 23:53:49 +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)
2014-04-17 04:18:25 +12:00
using Newtonsoft.Json;
2017-09-24 20:14:27 +13:00
using ShareX.HelpersLib;
2016-06-28 05:20:55 +12:00
using ShareX.UploadersLib.Properties;
2013-11-03 23:53:49 +13:00
using System.Collections.Generic;
using System.Collections.Specialized;
2016-06-28 05:20:55 +12:00
using System.Drawing;
2013-11-03 23:53:49 +13:00
using System.IO;
using System.Windows.Forms;
2013-11-03 23:53:49 +13:00
2014-12-11 09:25:20 +13:00
namespace ShareX.UploadersLib.FileUploaders
2013-11-03 23:53:49 +13:00
{
public class BoxFileUploaderService : FileUploaderService
{
public override FileDestination EnumValue { get; } = FileDestination.Box;
2016-06-28 05:20:55 +12:00
public override Icon ServiceIcon => Resources.Box;
public override bool CheckConfig(UploadersConfig config)
{
return OAuth2Info.CheckOAuth(config.BoxOAuth2Info);
}
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
{
return new Box(config.BoxOAuth2Info)
{
FolderID = config.BoxSelectedFolder.id,
Share = config.BoxShare,
ShareAccessLevel = config.BoxShareAccessLevel
};
}
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpBox;
}
2014-04-17 04:18:25 +12:00
public sealed class Box : FileUploader, IOAuth2
2013-11-03 23:53:49 +13:00
{
2014-04-17 08:35:10 +12:00
public static BoxFileEntry RootFolder = new BoxFileEntry
{
type = "folder",
id = "0",
2014-04-17 11:34:31 +12:00
name = "Root folder"
2014-04-17 08:35:10 +12:00
};
2014-04-17 04:18:25 +12:00
public OAuth2Info AuthInfo { get; set; }
2013-11-03 23:53:49 +13:00
public string FolderID { get; set; }
public bool Share { get; set; }
public BoxShareAccessLevel ShareAccessLevel { get; set; }
2013-11-03 23:53:49 +13:00
2014-04-17 04:18:25 +12:00
public Box(OAuth2Info oauth)
2013-11-03 23:53:49 +13:00
{
2014-04-17 04:18:25 +12:00
AuthInfo = oauth;
2013-11-03 23:53:49 +13:00
FolderID = "0";
Share = true;
ShareAccessLevel = BoxShareAccessLevel.Open;
2013-11-03 23:53:49 +13:00
}
2014-04-17 04:18:25 +12:00
public string GetAuthorizationURL()
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("response_type", "code");
args.Add("client_id", AuthInfo.Client_ID);
return URLHelpers.CreateQueryString("https://www.box.com/api/oauth2/authorize", args);
2014-04-17 04:18:25 +12:00
}
public bool GetAccessToken(string pin)
2013-11-03 23:53:49 +13:00
{
Dictionary<string, string> args = new Dictionary<string, string>();
2014-04-17 04:18:25 +12:00
args.Add("grant_type", "authorization_code");
args.Add("code", pin);
args.Add("client_id", AuthInfo.Client_ID);
args.Add("client_secret", AuthInfo.Client_Secret);
2013-11-03 23:53:49 +13:00
string response = SendRequestMultiPart("https://www.box.com/api/oauth2/token", args);
2013-11-03 23:53:49 +13:00
if (!string.IsNullOrEmpty(response))
{
2014-04-17 04:18:25 +12:00
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
2013-11-03 23:53:49 +13:00
2014-04-17 04:18:25 +12:00
if (token != null && !string.IsNullOrEmpty(token.access_token))
2013-11-03 23:53:49 +13:00
{
2014-04-17 04:18:25 +12:00
token.UpdateExpireDate();
AuthInfo.Token = token;
return true;
2013-11-03 23:53:49 +13:00
}
}
2014-04-17 04:18:25 +12:00
return false;
2013-11-03 23:53:49 +13:00
}
2014-04-17 04:18:25 +12:00
public bool RefreshAccessToken()
2013-11-03 23:53:49 +13:00
{
2014-04-17 04:18:25 +12:00
if (OAuth2Info.CheckOAuth(AuthInfo) && !string.IsNullOrEmpty(AuthInfo.Token.refresh_token))
2013-11-03 23:53:49 +13:00
{
2014-04-17 04:18:25 +12:00
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("grant_type", "refresh_token");
args.Add("refresh_token", AuthInfo.Token.refresh_token);
args.Add("client_id", AuthInfo.Client_ID);
args.Add("client_secret", AuthInfo.Client_Secret);
string response = SendRequestMultiPart("https://www.box.com/api/oauth2/token", args);
2014-04-17 04:18:25 +12:00
if (!string.IsNullOrEmpty(response))
{
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
if (token != null && !string.IsNullOrEmpty(token.access_token))
{
token.UpdateExpireDate();
AuthInfo.Token = token;
return true;
}
}
2013-11-03 23:53:49 +13:00
}
2014-04-17 04:18:25 +12:00
return false;
2013-11-03 23:53:49 +13:00
}
private NameValueCollection GetAuthHeaders()
{
NameValueCollection headers = new NameValueCollection();
headers.Add("Authorization", "Bearer " + AuthInfo.Token.access_token);
return headers;
}
2014-04-17 04:18:25 +12:00
public bool CheckAuthorization()
2013-11-03 23:53:49 +13:00
{
2014-04-17 04:18:25 +12:00
if (OAuth2Info.CheckOAuth(AuthInfo))
2013-11-03 23:53:49 +13:00
{
2014-04-17 04:18:25 +12:00
if (AuthInfo.Token.IsExpired && !RefreshAccessToken())
2013-11-03 23:53:49 +13:00
{
2014-04-17 04:18:25 +12:00
Errors.Add("Refresh access token failed.");
return false;
2013-11-03 23:53:49 +13:00
}
}
2014-04-17 04:18:25 +12:00
else
{
Errors.Add("Box login is required.");
return false;
}
2013-11-03 23:53:49 +13:00
2014-04-17 04:18:25 +12:00
return true;
2013-11-03 23:53:49 +13:00
}
2014-04-17 08:35:10 +12:00
public BoxFileInfo GetFiles(BoxFileEntry folder)
2013-11-03 23:53:49 +13:00
{
2014-04-17 08:35:10 +12:00
return GetFiles(folder.id);
2013-11-03 23:53:49 +13:00
}
2014-04-17 08:35:10 +12:00
public BoxFileInfo GetFiles(string id)
2013-11-03 23:53:49 +13:00
{
2014-04-17 11:34:31 +12:00
if (!CheckAuthorization())
{
return null;
}
2014-04-17 08:35:10 +12:00
string url = string.Format("https://api.box.com/2.0/folders/{0}/items", id);
2013-11-03 23:53:49 +13:00
string response = SendRequest(HttpMethod.GET, url, headers: GetAuthHeaders());
2013-11-03 23:53:49 +13:00
2014-04-17 08:35:10 +12:00
if (!string.IsNullOrEmpty(response))
{
return JsonConvert.DeserializeObject<BoxFileInfo>(response);
2013-11-03 23:53:49 +13:00
}
return null;
}
public string CreateSharedLink(string id, BoxShareAccessLevel accessLevel)
{
2021-06-10 10:14:01 +12:00
string response = SendRequest(HttpMethod.PUT, "https://api.box.com/2.0/files/" + id, "{\"shared_link\": {\"access\": \"" + accessLevel.ToString().ToLower() + "\"}}", headers: GetAuthHeaders());
if (!string.IsNullOrEmpty(response))
{
BoxFileEntry fileEntry = JsonConvert.DeserializeObject<BoxFileEntry>(response);
if (fileEntry != null && fileEntry.shared_link != null)
{
return fileEntry.shared_link.url;
}
}
return null;
}
2013-11-03 23:53:49 +13:00
public override UploadResult Upload(Stream stream, string fileName)
{
2014-04-17 04:18:25 +12:00
if (!CheckAuthorization())
2013-11-03 23:53:49 +13:00
{
return null;
}
2014-04-17 08:35:10 +12:00
if (string.IsNullOrEmpty(FolderID))
{
FolderID = "0";
}
2013-11-03 23:53:49 +13:00
Dictionary<string, string> args = new Dictionary<string, string>();
2014-04-17 04:18:25 +12:00
args.Add("parent_id", FolderID);
UploadResult result = SendRequestFile("https://upload.box.com/api/2.0/files/content", stream, fileName, "filename", args, GetAuthHeaders());
2013-11-03 23:53:49 +13:00
if (result.IsSuccess)
{
BoxFileInfo fileInfo = JsonConvert.DeserializeObject<BoxFileInfo>(result.Response);
2013-11-03 23:53:49 +13:00
if (fileInfo != null && fileInfo.entries != null && fileInfo.entries.Length > 0)
2013-11-03 23:53:49 +13:00
{
2014-04-17 08:35:10 +12:00
BoxFileEntry fileEntry = fileInfo.entries[0];
if (Share)
2013-11-03 23:53:49 +13:00
{
2014-04-17 08:35:10 +12:00
AllowReportProgress = false;
result.URL = CreateSharedLink(fileEntry.id, ShareAccessLevel);
}
else
{
2014-04-17 08:35:10 +12:00
result.URL = string.Format("https://app.box.com/files/0/f/{0}/1/f_{1}", fileEntry.parent.id, fileEntry.id);
2013-11-03 23:53:49 +13:00
}
}
}
return result;
}
}
public class BoxFileInfo
{
public BoxFileEntry[] entries { get; set; }
}
public class BoxFileEntry
{
2014-04-17 08:35:10 +12:00
public string type { get; set; }
public string id { get; set; }
2014-04-17 08:35:10 +12:00
public string sequence_id { get; set; }
public string etag { get; set; }
public string name { get; set; }
public BoxFileSharedLink shared_link { get; set; }
2014-04-17 08:35:10 +12:00
public BoxFileEntry parent { get; set; }
}
public class BoxFileSharedLink
{
public string url { get; set; }
}
2013-11-03 23:53:49 +13:00
public class BoxFolder
{
public string ID;
public string Name;
public string User_id;
public string Description;
public string Shared;
public string Shared_link;
public string Permissions;
//public List<BoxTag> Tags;
//public List<BoxFile> Files;
public List<BoxFolder> Folders = new List<BoxFolder>();
}
}