Преглед на файлове

feat(hiTeachSideMenu): add ContentService

terry преди 2 месеца
родител
ревизия
6f380d5f86
променени са 1 файла, в които са добавени 160 реда и са изтрити 0 реда
  1. 160 0
      TEAMModelOS/Services/ContentService.cs

+ 160 - 0
TEAMModelOS/Services/ContentService.cs

@@ -0,0 +1,160 @@
+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("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("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}"));
+        }
+    }
+}