using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SWS.CAD.Models;
using SWS.CAD.Models.NoEntity;
using SWS.Commons;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
namespace SWS.CAD.Services
{
public class AnnexesService : HttpService
{
public AnnexesService() : base()
{
}
///
/// 查询文件实体
///
///
public async Task GetAnnexesFile(string folderId)
{
var res = await this.GetAsync($"AnnexesApi/GetFileEntity?projectId={GlobalObject.curProject.ProjectId}&folderId={folderId}");
if (res.code == 200)
{
return res.data;
}
else
{
return null;//ERROR INFO
}
}
///
/// 上传本地文件到server
///
/// 完整的路径
/// 带后缀
/// 返回新的一个folder ID
public async Task UploadFile(string fullFilePath, string fileName)
{
using (var stream = new FileStream(fullFilePath, FileMode.Open, FileAccess.Read))
{
var res = await this.PostFileAsync($"AnnexesApi/UploadFile", stream, fileName);
return res;//new Folder Id
}
}
///
/// 下载文件
///
/// 文件下载地址
/// 本地保存路径
/// 进度报告接口(可选)
/// 取消令牌(可选)
/// 返回空字符串认为是OK的
public async Task DownloadFile(
string savePath, string fileId,
IProgress progress = null,
CancellationToken cancellationToken = default)
{
try
{
var p = Path.GetDirectoryName(savePath);
if (!Directory.Exists(p))
Directory.CreateDirectory(p);
string url = $"AnnexesApi/DownFile?fileId={fileId}";
// 发送请求并获取响应
using (var response = await GlobalObject.client.GetAsync(
url,
HttpCompletionOption.ResponseHeadersRead,
cancellationToken))
{
if (response.StatusCode != HttpStatusCode.OK)
{
string errorMsg = $"服务器地址 [{url}] 文件下载失败, 返回HTTP代码:" + response.StatusCode;
LoggerHelper.Current.Error(errorMsg);
throw new HttpRequestException(errorMsg);
}
response.EnsureSuccessStatusCode(); // 确保响应成功
var result = await response.Content.ReadAsStringAsync();
learunHttpRes