using System;
using System.Collections.Generic;
using System.Text;
using COSXML.Common;
using COSXML.CosException;
using System.IO;
namespace COSXML.Model.Object
{
///
/// 下载对象
///
///
public sealed class GetObjectRequest : ObjectRequest
{
///
/// 保存文件的本地文件夹路径
///
private string localDir;
///
/// 保存文件的本地的文件名
///
private string localFileName;
///
/// 保存文件的本地偏移位置,下载的数据从此处开始append该文件后面
///
private long localFileOffset = 0;
///
/// 下载进度回调
///
private COSXML.Callback.OnProgressCallback progressCallback;
public GetObjectRequest(string bucket, string key, string localDir, string localFileName)
: base(bucket, key)
{
this.method = CosRequestMethod.GET;
this.localDir = localDir;
this.localFileName = localFileName;
}
///
/// 下载进度回调
///
///
public void SetCosProgressCallback(COSXML.Callback.OnProgressCallback progressCallback)
{
this.progressCallback = progressCallback;
}
internal COSXML.Callback.OnProgressCallback GetCosProgressCallback()
{
return progressCallback;
}
///
/// 保存文件的本地偏移位置,下载的数据从此处开始append该文件后面
///
///
public void SetLocalFileOffset(long localFileOffset)
{
this.localFileOffset = localFileOffset > 0 ? localFileOffset : 0;
}
public long GetLocalFileOffset()
{
return localFileOffset;
}
///
/// 下载内容范围
///
///
///
public void SetRange(long start, long end)
{
if (start < 0) return;
if (end < start) end = -1;
SetRequestHeader(CosRequestHeaderKey.RANGE, String.Format("bytes={0}-{1}", start,
(end == -1 ? "" : end.ToString())));
}
///
/// 下载内容的起始偏移量
///
///
public void SetRange(long start)
{
SetRange(start, -1);
}
///
/// 最大下载速度,单位是 bit/s
///
///
public void LimitTraffic(long rate) {
SetRequestHeader(CosRequestHeaderKey.X_COS_TRAFFIC_LIMIT, rate.ToString());
}
///
/// 下载特定版本的对象
///
///
public void SetVersionId(string versionId)
{
if (versionId != null)
{
SetQueryParameter(CosRequestHeaderKey.VERSION_ID, versionId);
}
}
///
/// 响应头部中的 Content-Type 参数
///
///
public void SetResponseContentType(string responseContentType)
{
if (responseContentType != null)
{
SetQueryParameter(CosRequestHeaderKey.RESPONSE_CONTENT_TYPE, responseContentType);
}
}
///
/// 响应头部中的 Content-Language 参数
///
///
public void SetResponseContentLanguage(string responseContentLanguage)
{
if (responseContentLanguage != null)
{
SetQueryParameter(CosRequestHeaderKey.RESPONSE_CONTENT_LANGUAGE, responseContentLanguage);
}
}
///
/// 响应头部中的 Cache-Control 参数
///
///
public void SetResponseCacheControl(string responseCacheControl)
{
if (responseCacheControl != null)
{
SetQueryParameter(CosRequestHeaderKey.RESPONSE_CACHE_CONTROL, responseCacheControl);
}
}
///
/// 响应头部中的 Content-Disposition 参数
///
///
public void SetResponseContentDisposition(string responseDisposition)
{
if (responseDisposition != null)
{
SetQueryParameter(CosRequestHeaderKey.RESPONSE_CONTENT_DISPOSITION, responseDisposition);
}
}
///
/// 响应头部中的 Content-Encoding 参数
///
///
public void SetResponseContentEncoding(string responseContentEncoding)
{
if (responseContentEncoding != null)
{
SetQueryParameter(CosRequestHeaderKey.RESPONSE_CONTENT_ENCODING, responseContentEncoding);
}
}
///
/// 响应头部中的 Content-Expires 参数
///
///
public void SetResponseExpires(string responseExpires)
{
if (responseExpires != null)
{
SetQueryParameter(CosRequestHeaderKey.RESPONSE_EXPIRES, responseExpires);
}
}
public override void CheckParameters()
{
if (localDir == null) throw new CosClientException((int)CosClientError.INVALID_ARGUMENT, "localDir = null");
if(requestUrlWithSign != null && localFileName == null) throw new CosClientException((int)CosClientError.INVALID_ARGUMENT, "localFileName = null");
base.CheckParameters();
}
///
/// 获取本地文件保存路径
///
///
public string GetSaveFilePath()
{
string result = localDir;
DirectoryInfo dirInfo = new DirectoryInfo(localDir);
if (!dirInfo.Exists)
{
dirInfo.Create();
}
if (String.IsNullOrEmpty(localFileName)) localFileName = path;
if (localDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString(), StringComparison.OrdinalIgnoreCase))
{
result = result + localFileName;
}
else
{
result = result + System.IO.Path.DirectorySeparatorChar + localFileName;
}
return result;
}
}
}