using Ewide.Core; using Furion.DynamicApiController; using Furion.VirtualFileServer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using NPOI.OpenXml4Net.OPC.Internal; using OBS; using OBS.Model; using OnceMi.AspNetCore.OSS; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using Vote.Services.Dto; namespace Vote.Services.ApiController { /// /// OBS /// [ApiDescriptionSettings("huawei", Order = 0)] [Route("/huawei")] [AllowAnonymous] public class HuaweiService : IDynamicApiController { private readonly IOSSService _OSSService; private readonly string _bucketName; private readonly string filePrefix; /// /// /// /// public HuaweiService(IOSSService OSSService) { _OSSService = OSSService; _bucketName = "94.229"; filePrefix = "D:\\obsFile\\"; } /// /// 列出当前账号下允许访问的所有储存桶。 /// /// public async Task ListBuckets() { try { var result = await _OSSService.ListBucketsAsync(); return result; } catch (Exception ex) { throw; } } /// /// 获取储存桶的外部访问权限。 /// /// public async Task GetBucketAclAsync() { try { var result = await _OSSService.GetBucketAclAsync(_bucketName); return result; } catch (Exception ex) { throw; } } /// /// 获取指定储存桶中指定对象是否存在。 /// /// public async Task ObjectsExistsAsync([FromBody] GetObjectInput args) { if (args == null || string.IsNullOrWhiteSpace(args.objectName)) return await Display(false, "error"); try { var objectName = args.objectName; if (objectName.ToLower().EndsWith(".dwg")) { objectName += ".zip"; } var result = await _OSSService.ObjectsExistsAsync(_bucketName, objectName); return result; } catch (Exception ex) { throw; } } /// /// 获取文件的数据流。 大小写敏感! /// /// [HttpPost] public async Task DownloadObjectAsync([FromBody] GetObjectInput args) { if (args == null || string.IsNullOrWhiteSpace(args.objectName)) return await Display(false, "error"); var objectName = args.objectName; if (objectName.ToLower().EndsWith(".dwg")) { objectName += ".zip"; } #region MyRegion //FileStreamResult fileStreamResult = null; //await _OSSService.GetObjectAsync(_bucketName, objectName, (stream) => //{ // if (FS.TryGetContentType(objectName, out var contentType) || GetContentType(objectName, out contentType)) // fileStreamResult = new FileStreamResult(stream, contentType) { FileDownloadName = objectName }; //}); //if (fileStreamResult != null) // return fileStreamResult; //else // return await Display(false, "文件格式有问题"); //var localPath = filePrefix + _bucketName + '/' + args.objectName; //await _OSSService.GetObjectAsync(_bucketName, args.objectName, localPath); //if (FS.TryGetContentType(args.objectName, out var contentType) || GetContentType(args.objectName, out contentType)) //{ // return new FileStreamResult(new FileStream(localPath, FileMode.Open), contentType) { FileDownloadName = args.objectName }; //} //else //{ // return await Display(false, "文件格式有问题"); //} #endregion #region 原始华为sdk try { //var objectName = "/files/coc/202011/11/2020kdfj0075_5.pdf"; ObsClient client = new ObsClient(_OSSService.Options.AccessKey, _OSSService.Options.SecretKey, _OSSService.Options.Endpoint); GetObjectRequest request = new GetObjectRequest() { BucketName = _bucketName, ObjectKey = objectName, }; using (GetObjectResponse response = client.GetObject(request)) { if (FS.TryGetContentType(objectName, out var contentType) || GetContentType(objectName, out contentType)) { //var localPath = filePrefix + _bucketName + '/' + objectName; //if (!File.Exists(localPath)) //{ // response.WriteResponseStreamToFile(localPath); //} //return new FileStreamResult(new FileStream(localPath, FileMode.Open), contentType) { FileDownloadName = objectName }; var ms = new MemoryStream(); await response.OutputStream.CopyToAsync(ms); ms.Position = 0; response.OutputStream.Close(); response.OutputStream.Dispose(); return new FileStreamResult(ms, contentType) { FileDownloadName = objectName }; } else { return await Display(false, "文件格式有问题"); } } } catch (ObsException ex) { throw; } #endregion } private bool GetContentType(string filename, out string contenttype) { contenttype = ""; bool rslt = true; if (filename.ToLower().EndsWith(".dwg")) { contenttype = "application/octet-stream"; } else rslt = false; return rslt; } /// /// 上传文件的数据流。 大小写敏感! /// /// [HttpPost] [DisableRequestSizeLimit] public async Task PutObjectAsync([FromBody] PutObjectInput args) { if (args == null || string.IsNullOrWhiteSpace(args.path)) return new ObsApiOutput(false, "参数为空"); var file = new FileInfo(args.path); if (!file.Exists) return new ObsApiOutput(false, "文件不存在"); var objectName = args.objectName; var path = args.path; if (string.IsNullOrWhiteSpace(args.objectName)) objectName = file.Name; try { #region dwg压缩处理 var zipRslt = false; if (path.ToLower().EndsWith(".dwg")) { if (new Tools.ZipHelper().ZipOneFile(path, 0)) { zipRslt = true; path += ".zip"; objectName += ".zip"; } } #endregion var r = await _OSSService.PutObjectAsync(_bucketName, objectName, path); if (r && zipRslt) { new FileInfo(path).Delete(); } return new ObsApiOutput(r, r ? "上传成功" : "上传失败"); } catch (ObsException ex) { return new ObsApiOutput(false, "错误状态码:" + ex.StatusCode); } catch (Exception ex) { return new ObsApiOutput(false, "异常信息:" + ex.Message + ex.StackTrace); } } private async Task Display(bool isSuccess, object data) { return DisplayJson(new RestfulResult { Code = isSuccess ? 200 : 500, // 处理没有返回值情况 204 Success = true, Data = data, Message = "请求成功", Extras = null, Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }); } private IActionResult DisplayJson(object data) { return new ContentResult { Content = JsonConvert.SerializeObject(data, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(), DateFormatString = "yyyy-MM-dd HH:mm:ss" }), ContentType = "application/json" }; } //public async Task Test() //{ // // 创建ObsClient实例 // ObsClient client = new ObsClient("C4D30C2801D928AAF687", "ooZVXaB1tqIz7DHTv53RILD7o5cAAAGAAdkoqlR2", "http://10.74.25.87:6020"); // // 列举桶 // try // { // ListBucketsRequest request = new ListBucketsRequest(); // ListBucketsResponse response = client.ListBuckets(request); // return response; // } // catch (ObsException ex) // { // throw; // } //} } }