| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using CounterDrone.Core.Models; |
| | | 5 | | using CounterDrone.Core.Repository; |
| | | 6 | | |
| | | 7 | | namespace CounterDrone.Core.Services |
| | | 8 | | { |
| | | 9 | | public class ModelService : IModelService |
| | | 10 | | { |
| | | 11 | | private readonly ModelRepository _repo; |
| | | 12 | | private readonly IPathProvider _paths; |
| | | 13 | | |
| | 7 | 14 | | public ModelService(ModelRepository repo, IPathProvider paths) |
| | 7 | 15 | | { |
| | 7 | 16 | | _repo = repo; |
| | 7 | 17 | | _paths = paths; |
| | 7 | 18 | | } |
| | | 19 | | |
| | | 20 | | public ModelInfo ImportModel(string filePath, string name) |
| | 11 | 21 | | { |
| | 11 | 22 | | if (string.IsNullOrWhiteSpace(name)) |
| | 1 | 23 | | throw new ArgumentException("模型名称不能为空"); |
| | | 24 | | |
| | 10 | 25 | | var ext = Path.GetExtension(filePath).ToLowerInvariant(); |
| | 10 | 26 | | if (ext != ".fbx" && ext != ".obj" && ext != ".stl" && ext != ".glb" && ext != ".gltf") |
| | 1 | 27 | | throw new ArgumentException($"不支持的模型格式:{ext}"); |
| | | 28 | | |
| | 9 | 29 | | var fileInfo = new FileInfo(filePath); |
| | 9 | 30 | | if (fileInfo.Length > 500 * 1024 * 1024) |
| | 0 | 31 | | throw new ArgumentException("模型文件大小超过 500MB 限制"); |
| | | 32 | | |
| | 9 | 33 | | var model = new ModelInfo |
| | 9 | 34 | | { |
| | 9 | 35 | | Name = name, |
| | 9 | 36 | | FilePath = filePath, |
| | 9 | 37 | | FileSize = fileInfo.Length / (1024.0 * 1024.0), |
| | 9 | 38 | | }; |
| | | 39 | | |
| | 9 | 40 | | _repo.Insert(model); |
| | 9 | 41 | | return model; |
| | 9 | 42 | | } |
| | | 43 | | |
| | | 44 | | public void DeleteModel(string id) |
| | 1 | 45 | | { |
| | 1 | 46 | | _repo.Delete(id); |
| | 1 | 47 | | } |
| | | 48 | | |
| | | 49 | | public List<ModelInfo> GetAllModels() |
| | 1 | 50 | | { |
| | 1 | 51 | | return _repo.GetAll(); |
| | 1 | 52 | | } |
| | | 53 | | |
| | | 54 | | public ModelInfo GetModel(string id) |
| | 2 | 55 | | { |
| | 2 | 56 | | return _repo.GetById(id); |
| | 2 | 57 | | } |
| | | 58 | | } |
| | | 59 | | } |