using System.Threading.Tasks;
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
using TEAMModelOS.SDK.Module.AzureBlob.Container;
using TEAMModelOS.SDK.Context.Constant;
using TEAMModelOS.SDK.Helper.Security.ShaHash;
using System;
using System.IO;
using Azure.Storage.Blobs.Specialized;
using System.Collections.Generic;
using System.Linq;
namespace TEAMModelOS.SDK.DI
{
public static class AzureStorageBlobExtensions
{
///
/// 取得指定前置詞的 Blob 名稱的總大小(Bytes),例如指定目錄名稱為前置詞
///
/// 篩選開頭名稱,Null代表容器總大小
/// 總大小(Bytes),如果為Null代表查無前置詞或者發生錯誤
public static async Task GetBlobsSize(this BlobContainerClient client, string prefix = null)
{
long? size = 0;
try
{
await foreach (var item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix))
{
size += item.Properties.ContentLength;
};
return size;
}
catch
{
return size;
}
}
///
/// https://teammodelstorage.blob.core.chinacloudapi.cn/ydzt/112315401795%2F
/// 批量刪除Blobs
///
/// 篩選開頭名稱,Null代表容器
public static async Task DelectBlobs(this BlobServiceClient client, string blobContainerName, string prefix = null)
{
if (string.IsNullOrWhiteSpace(prefix)) return false;
try
{
BlobContainerClient bcc = client.GetBlobContainerClient(blobContainerName);
BlobBatchClient bbc = client.GetBlobBatchClient();
List blobs = new List();
await foreach (var item in bcc.GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix))
{
var urib = new UriBuilder(bcc.Uri);
urib.Path += "/" + item.Name;
blobs.Add(urib.Uri);
};
if (blobs.Count <= 256)
{
await bbc.DeleteBlobsAsync(blobs);
return true;
}
else
{
int pages = (blobs.Count + 255) / 256; //256是批量操作最大值,pages = (total + max -1) / max;
for (int i = 0; i < pages; i++)
{
List lists = blobs.Skip((i) * 256).Take(256).ToList();
await bbc.DeleteBlobsAsync(lists);
}
return true;
}
}
catch
{
return false;
}
}
///
/// 系统管理员 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
/// "system": [ "res", "item", "htex", "task", "info", "room", "exam" ],
/// 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
/// "school": [ "res", "item", "htex", "task", "info", "room", "exam" ],
/// 资源,题目关联,htex关联,学习活动关联,教师基本信息关联
/// "teacher": [ "res", "item", "htex", "task", "info" ],
/// 答案及学习活动上传的文件,学生基本信息关联
///"student": [ "stu/{studentId}/ans", "stu/{studentId}/task" ]
///
/// 容器名称
/// 文件内容的流
/// 业务文件夹
/// 文件名
/// 是否存放文件后缀对应的contentType
///
public static async Task UploadFileByContainer(this AzureStorageFactory azureStorage, string name, string text, string folder, string fileName, bool contentTypeDefault = true)
{
// string groupName =folder;
BlobContainerClient blobContainer = azureStorage.GetBlobContainerClient(name.ToLower().Replace("#", "")); //blobClient.GetContainerReference(groupName);
Uri url = blobContainer.Uri;
var blockBlob = blobContainer.GetBlobClient(folder + "/" + fileName);
string fileext = fileName.Substring(fileName.LastIndexOf(".") > 0 ? fileName.LastIndexOf(".") : 0);
string content_type = "application/octet-stream";
if (!contentTypeDefault)
{
ContentTypeDict.dict.TryGetValue(fileext, out string contenttype);
if (!string.IsNullOrEmpty(contenttype))
{
content_type = contenttype;
}
else
{
//blockBlob.Properties.ContentType = content_type;
}
}
else
{
//blockBlob.Properties.ContentType = content_type;
}
byte[] bytes = System.Text.Encoding.Default.GetBytes(text);
Stream streamBlob = new MemoryStream(bytes);
await blockBlob.UploadAsync(streamBlob, true);
blockBlob.SetHttpHeaders(new BlobHttpHeaders { ContentType = content_type });
AzureBlobModel model = new AzureBlobModel(fileName, name, folder, fileName, folder, content_type, bytes.Length)
{
Sha1Code = ShaHashHelper.GetSHA1(bytes),
BlobUrl = blockBlob.Uri.ToString()
};
return model;
}
///
/// 系统管理员 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
/// "system": [ "res", "item", "htex", "task", "info", "room", "exam" ],
/// 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
/// "school": [ "res", "item", "htex", "task", "info", "room", "exam" ],
/// 资源,题目关联,htex关联,学习活动关联,教师基本信息关联
/// "teacher": [ "res", "item", "htex", "task", "info" ],
/// 答案及学习活动上传的文件,学生基本信息关联
///"student": [ "stu/{studentId}/ans", "stu/{studentId}/task" ]
///
/// 容器名称
/// 文件内容的流
/// 业务文件夹
/// 文件名
/// 是否存放文件后缀对应的contentType
///
public static async Task UploadFileByContainer(this AzureStorageFactory azureStorage, string name, Stream stream, string folder, string fileName, bool contentTypeDefault = true)
{
// string groupName =folder;
BlobContainerClient blobContainer = azureStorage.GetBlobContainerClient(name.ToLower().Replace("#", "")); //blobClient.GetContainerReference(groupName);
Uri url = blobContainer.Uri;
var blockBlob = blobContainer.GetBlobClient(folder + "/" + fileName);
string fileext = fileName.Substring(fileName.LastIndexOf(".") > 0 ? fileName.LastIndexOf(".") : 0);
string content_type = "application/octet-stream";
if (!contentTypeDefault)
{
ContentTypeDict.dict.TryGetValue(fileext, out string contenttype);
if (!string.IsNullOrEmpty(contenttype))
{
content_type = contenttype;
}
else
{
//blockBlob.Properties.ContentType = content_type;
}
}
else
{
//blockBlob.Properties.ContentType = content_type;
}
await blockBlob.UploadAsync(stream, true);
blockBlob.SetHttpHeaders(new BlobHttpHeaders { ContentType = content_type });
AzureBlobModel model = new AzureBlobModel(fileName, name, folder, fileName, folder, content_type, stream.Length)
{
Sha1Code = ShaHashHelper.GetSHA1(stream),
BlobUrl = blockBlob.Uri.ToString()
};
return model;
}
}
}