using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using FastReport.Export;
using FastReport.Utils;
namespace FastReport.Cloud.StorageClient.Dropbox
{
///
/// Dropbox cloud storage client.
///
public class DropboxStorageClient : CloudStorageClient
{
#region Constants
///
/// The base URL for files_put command.
///
public const string FILES_PUT_URL_BASE = "https://content.dropboxapi.com/2/files/upload";
#endregion // Constants
#region Fields
private string accessToken;
#endregion // Fields
#region Properties
///
/// Gets or sets the application access token.
///
public string AccessToken
{
get { return accessToken; }
set { accessToken = value; }
}
#endregion // Properties
#region Constructors
///
/// Initializes a new instance of the class.
///
public DropboxStorageClient() : base()
{
accessToken = "";
}
///
/// Initializes a new instance of the class.
///
/// The Dropbox application access token.
public DropboxStorageClient(string accessToken) : base()
{
this.accessToken = accessToken;
}
#endregion // Constructors
#region Protected Methods
///
protected override void SaveMemoryStream(MemoryStream ms)
{
try
{
System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
HttpWebRequest request = WebRequest.Create(FILES_PUT_URL_BASE) as HttpWebRequest;
request.Method = HttpMethod.Post;
RequestUtils.SetProxySettings(request, ProxySettings);
request.Headers.Add("Authorization", "Bearer " + accessToken);
request.Headers.Add("Dropbox-API-Arg", "{\"path\": \"/" + Filename + "\",\"mode\": \"overwrite\",\"autorename\": false,\"mute\": false}");
request.ContentType = "application/octet-stream";
int msLength = Convert.ToInt32(ms.Length);
byte[] msBuffer = new byte[msLength];
ms.Read(msBuffer, 0, msLength);
List content = new List();
content.AddRange(msBuffer);
int length = content.Count;
byte[] buffer = new byte[length];
buffer = content.ToArray();
request.ContentLength = buffer.Length;
using (Stream rs = request.GetRequestStream())
{
rs.Write(buffer, 0, buffer.Length);
}
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
}
catch (Exception ex)
{
throw new CloudStorageException(ex.Message, ex);
}
}
#endregion // Protected Methods
}
}