123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.Azure.Cosmos;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Models;
- namespace TEAMModelOS.Services
- {
- public class ContentService
- {
- private static readonly Dictionary<string, List<string>> fileTypes = new()
- {
- { "audio", new List<string> { "mp3", "ogg", "wav", "ape", "cda", "au", "midi", "mac", "aac" }},
- { "doc", new List<string> { "ppt", "pptx", "doc", "docx", "pdf", "xls", "xlsx", "csv" }},
- { "image", new List<string> { "jpg", "jpeg", "bmp", "tif", "png", "gif", "svg" }},
- { "res", new List<string> { "hte", "htex" }},
- { "video", new List<string> { "rmvb", "wmv", "asf", "avi", "3gp", "mpg", "mkv", "mp4", "dvd", "ogm", "mov", "mpeg2", "mpeg4" }},
- };
- private readonly AzureStorageFactory _azureStorage;
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly FileExtractService _fileExtractService;
- public ContentService(AzureStorageFactory azureStorage, AzureCosmosFactory azureCosmos)
- {
- _azureStorage = azureStorage;
- _azureCosmos = azureCosmos;
- _fileExtractService = new FileExtractService(azureStorage);
- }
- public async Task UploadToSchool(IFormFile file, string id, string[] periodId, string[] subjectId, string[] gradeId)
- {
- var fileName = file.FileName;
- var extension = Path.GetExtension(fileName).ToLower().TrimStart('.');
- var type = _getFileType(extension);
- var blobPath = $"{type}/{fileName}";
- await _uploadFile(file, extension, id, blobPath);
- if (type == "image")
- {
- var thumbnailPath = $"thum/{fileName}";
- await _uploadThumbnail(file, id, thumbnailPath);
- }
- var bloblog = new Bloblog
- {
- id = Guid.NewGuid().ToString(),
- pk = "Bloblog",
- code = $"Bloblog-{id}",
- time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
- size = file.Length,
- url = blobPath,
- type = type,
- ext = "." + extension,
- periodId = periodId.ToList(),
- subjectId = subjectId.ToList(),
- gradeId = gradeId.ToList(),
- };
- await _writeFileRecord(Constant.School, id, bloblog);
- }
- public async Task UploadToPrivate(IFormFile file, string id)
- {
- var fileName = file.FileName;
- var extension = Path.GetExtension(fileName).ToLower().TrimStart('.');
- var type = _getFileType(extension);
- var blobPath = $"{type}/{fileName}";
- await _uploadFile(file, extension, id, blobPath);
- if (type == "image")
- {
- var thumbnailPath = $"thum/{fileName}";
- await _uploadThumbnail(file, id, thumbnailPath);
- }
- var bloblog = new Bloblog
- {
- id = Guid.NewGuid().ToString(),
- pk = "Bloblog",
- code = $"Bloblog-{id}",
- time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
- size = file.Length,
- url = blobPath,
- type = type,
- ext = "." + extension,
- periodId = new List<string> {},
- subjectId = new List<string> {},
- gradeId = new List<string> {},
- };
- await _writeFileRecord(Constant.Teacher, id, bloblog);
- }
- private string _getFileType(string extension)
- {
- foreach (var fileType in fileTypes)
- {
- if (fileType.Value.Contains(extension))
- {
- return fileType.Key;
- }
- }
- return "other";
- }
- private async Task _uploadFile(IFormFile file, string extension, string id, string path)
- {
- var fileName = Path.GetFileNameWithoutExtension(file.FileName);
- var blobClient = _azureStorage.GetBlobContainerClient(id).GetBlobClient(path);
- using (var stream = file.OpenReadStream())
- {
- if (extension == "htex")
- {
- await _fileExtractService.ExtractHTEX(id, fileName, stream);
- }
- else
- {
- await blobClient.UploadAsync(stream);
- }
- }
- }
- private async Task _uploadThumbnail(IFormFile file, string id, string path)
- {
- int width = 300;
- using (var stream = new MemoryStream())
- {
- await file.CopyToAsync(stream);
- stream.Seek(0, SeekOrigin.Begin);
- using (var image = Image.FromStream(stream))
- {
- int height = (int)(image.Height * (width / (double)image.Width));
- using (var resizedImage = new Bitmap(image, width, height))
- using (var thumbnailStream = new MemoryStream())
- {
- resizedImage.Save(thumbnailStream, ImageFormat.Jpeg);
- thumbnailStream.Position = 0;
- var blobClient = _azureStorage.GetBlobContainerClient(id).GetBlobClient(path);
- await blobClient.UploadAsync(thumbnailStream);
- }
- }
- }
- }
- private async Task _writeFileRecord(string containerName, string id, Bloblog bloblog)
- {
- var cosmosContainer = _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, containerName);
- await cosmosContainer.CreateItemAsync(bloblog, new PartitionKey($"Bloblog-{id}"));
- }
- }
- }
|