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
{
///
/// 保存文件的本地文件夹路径
///
public string localDir;
///
/// 保存文件的本地的文件名
///
public 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);
}
}
public override void CheckParameters()
{
if (localDir == null)
{
throw new CosClientException((int)CosClientError.InvalidArgument, "localDir = null");
}
if (requestUrlWithSign != null && localFileName == null)
{
throw new CosClientException((int)CosClientError.InvalidArgument, "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;
}
}
}