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
2014-01-12 20:25:51 +13:00
Copyright ( C ) 2008 - 2014 ShareX Developers
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 )
using HelpersLib ;
2014-04-17 04:18:25 +12:00
using Newtonsoft.Json ;
2013-11-03 23:53:49 +13:00
using System.Collections.Generic ;
using System.Collections.Specialized ;
using System.IO ;
using System.Xml.Linq ;
2014-04-17 04:18:25 +12:00
using UploadersLib.HelperClasses ;
2013-11-03 23:53:49 +13:00
namespace UploadersLib.FileUploaders
{
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 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 ; }
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 ;
}
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 CreateQuery ( "https://www.box.com/api/oauth2/authorize" , args ) ;
}
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
2014-04-17 05:55:07 +12:00
string response = SendRequest ( HttpMethod . POST , "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 ) ;
2014-04-17 05:55:07 +12:00
string response = SendRequest ( HttpMethod . POST , "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
}
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
}
public BoxFolder GetAccountTree ( string folderID = "0" , bool onelevel = false , bool nofiles = false , bool nozip = true , bool simple = false )
{
NameValueCollection args = new NameValueCollection ( ) ;
args . Add ( "action" , "get_account_tree" ) ;
args . Add ( "folder_id" , folderID ) ;
if ( onelevel ) // Make a tree of one level depth, so you will get only the files and folders stored in the folder of the folder_id you have provided.
{
args . Add ( "params" , "onelevel" ) ;
}
if ( nofiles ) // Only include the folders in the user account tree, and ignore the files.
{
args . Add ( "params" , "nofiles" ) ;
}
if ( nozip ) // Do not zip the tree xml.
{
args . Add ( "params" , "nozip" ) ;
}
if ( simple ) // Display the full tree with a limited list of attributes to make for smaller, more efficient output (folders only contain the 'name' and 'id' attributes, and files will contain the 'name', 'id', 'created', and 'size' attributes)
{
args . Add ( "params" , "simple" ) ;
}
2014-04-17 05:55:07 +12:00
string url = CreateQuery ( "" , args ) ;
2013-11-03 23:53:49 +13:00
2014-04-17 05:55:07 +12:00
string response = SendRequest ( HttpMethod . GET , url ) ;
2013-11-03 23:53:49 +13:00
if ( ! string . IsNullOrEmpty ( response ) )
{
XDocument xd = XDocument . Parse ( response ) ;
XElement xe = xd . GetElement ( "response" ) ;
if ( xe ! = null & & xe . GetElementValue ( "status" ) = = "listing_ok" )
{
XElement xeTree = xe . Element ( "tree" ) ;
if ( xeTree ! = null )
{
return ParseFolder ( xeTree . Element ( "folder" ) ) ;
}
}
}
return null ;
}
private BoxFolder ParseFolder ( XElement xe )
{
if ( xe ! = null & & xe . Name = = "folder" )
{
BoxFolder folder = new BoxFolder ( ) ;
folder . ID = xe . GetAttributeValue ( "id" ) ;
folder . Name = xe . GetAttributeValue ( "name" ) ;
XElement xeFolders = xe . Element ( "folders" ) ;
if ( xeFolders ! = null )
{
foreach ( XElement xeFolder in xeFolders . Elements ( ) )
{
BoxFolder childFolder = ParseFolder ( xeFolder ) ;
if ( childFolder ! = null )
{
folder . Folders . Add ( childFolder ) ;
}
}
}
return folder ;
}
return null ;
}
public BoxFolder GetFolderList ( )
{
return GetAccountTree ( "0" , false , true , true , true ) ;
}
2014-04-17 07:14:45 +12:00
public string CreateSharedLink ( string id )
2014-04-17 05:55:07 +12:00
{
2014-04-17 07:14:45 +12:00
NameValueCollection headers = new NameValueCollection ( ) ;
headers . Add ( "Authorization" , "Bearer " + AuthInfo . Token . access_token ) ;
string response = SendRequest ( HttpMethod . PUT , "https://api.box.com/2.0/files/" + id , "{\"shared_link\": {\"access\": \"open\"}}" , headers : headers ) ;
if ( ! string . IsNullOrEmpty ( response ) )
{
BoxFileEntry fileEntry = JsonConvert . DeserializeObject < BoxFileEntry > ( response ) ;
if ( fileEntry ! = null & & fileEntry . shared_link ! = null )
{
return fileEntry . shared_link . url ;
}
}
return null ;
2014-04-17 05:55:07 +12:00
}
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 ;
}
Dictionary < string , string > args = new Dictionary < string , string > ( ) ;
2014-04-17 04:18:25 +12:00
args . Add ( "parent_id" , FolderID ) ;
NameValueCollection headers = new NameValueCollection ( ) ;
headers . Add ( "Authorization" , "Bearer " + AuthInfo . Token . access_token ) ;
2013-11-03 23:53:49 +13:00
2014-04-17 04:18:25 +12:00
UploadResult result = UploadData ( stream , "https://upload.box.com/api/2.0/files/content" , fileName , "filename" , args , headers : headers ) ;
2013-11-03 23:53:49 +13:00
if ( result . IsSuccess )
{
2014-04-17 07:14:45 +12:00
BoxFileInfo fileInfo = JsonConvert . DeserializeObject < BoxFileInfo > ( result . Response ) ;
2013-11-03 23:53:49 +13:00
2014-04-17 07:14:45 +12:00
if ( fileInfo ! = null & & fileInfo . entries ! = null & & fileInfo . entries . Length > 0 )
2013-11-03 23:53:49 +13:00
{
2014-04-17 07:14:45 +12:00
if ( Share )
2013-11-03 23:53:49 +13:00
{
2014-04-17 07:14:45 +12:00
result . URL = CreateSharedLink ( fileInfo . entries [ 0 ] . id ) ;
}
else
{
result . URL = string . Format ( "https://app.box.com/files/0/f/{0}/1/f_{1}" , "folder" , "file" ) ;
2013-11-03 23:53:49 +13:00
}
}
}
return result ;
}
}
2014-04-17 07:14:45 +12:00
public class BoxFileInfo
{
public BoxFileEntry [ ] entries { get ; set ; }
}
public class BoxFileEntry
{
public string id { get ; set ; }
public string name { get ; set ; }
public BoxFileSharedLink shared_link { 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 > ( ) ;
}
}