|
| 1 | +using Microsoft.AspNetCore.StaticFiles; |
| 2 | +using System.IO; |
| 3 | +using System.Threading; |
| 4 | + |
| 5 | +namespace BotSharp.Core.Files; |
| 6 | + |
| 7 | +public partial class BotSharpFileService |
| 8 | +{ |
| 9 | + public IEnumerable<MessageFileModel> GetChatImages(string conversationId, List<RoleDialogModel> conversations, int offset = 1) |
| 10 | + { |
| 11 | + var files = new List<MessageFileModel>(); |
| 12 | + if (string.IsNullOrEmpty(conversationId) || conversations.IsNullOrEmpty()) |
| 13 | + { |
| 14 | + return files; |
| 15 | + } |
| 16 | + |
| 17 | + if (offset <= 0) |
| 18 | + { |
| 19 | + offset = MIN_OFFSET; |
| 20 | + } |
| 21 | + else if (offset > MAX_OFFSET) |
| 22 | + { |
| 23 | + offset = MAX_OFFSET; |
| 24 | + } |
| 25 | + |
| 26 | + var messageIds = conversations.Select(x => x.MessageId).Distinct().TakeLast(offset).ToList(); |
| 27 | + files = GetMessageFiles(conversationId, messageIds, imageOnly: true).ToList(); |
| 28 | + return files; |
| 29 | + } |
| 30 | + |
| 31 | + public IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds, bool imageOnly = false) |
| 32 | + { |
| 33 | + var files = new List<MessageFileModel>(); |
| 34 | + if (messageIds.IsNullOrEmpty()) return files; |
| 35 | + |
| 36 | + foreach (var messageId in messageIds) |
| 37 | + { |
| 38 | + var dir = GetConversationFileDirectory(conversationId, messageId); |
| 39 | + if (!ExistDirectory(dir)) |
| 40 | + { |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + foreach (var file in Directory.GetFiles(dir)) |
| 45 | + { |
| 46 | + var contentType = GetFileContentType(file); |
| 47 | + if (imageOnly && !_allowedTypes.Contains(contentType)) |
| 48 | + { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + var fileName = Path.GetFileNameWithoutExtension(file); |
| 53 | + var extension = Path.GetExtension(file); |
| 54 | + var fileType = extension.Substring(1); |
| 55 | + |
| 56 | + var model = new MessageFileModel() |
| 57 | + { |
| 58 | + MessageId = messageId, |
| 59 | + FileUrl = $"/conversation/{conversationId}/message/{messageId}/file/{fileName}", |
| 60 | + FileStorageUrl = file, |
| 61 | + FileName = fileName, |
| 62 | + FileType = fileType, |
| 63 | + ContentType = contentType |
| 64 | + }; |
| 65 | + files.Add(model); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return files; |
| 70 | + } |
| 71 | + |
| 72 | + public string GetMessageFile(string conversationId, string messageId, string fileName) |
| 73 | + { |
| 74 | + var dir = GetConversationFileDirectory(conversationId, messageId); |
| 75 | + if (!ExistDirectory(dir)) |
| 76 | + { |
| 77 | + return string.Empty; |
| 78 | + } |
| 79 | + |
| 80 | + var found = Directory.GetFiles(dir).FirstOrDefault(f => Path.GetFileNameWithoutExtension(f).IsEqualTo(fileName)); |
| 81 | + return found; |
| 82 | + } |
| 83 | + |
| 84 | + public bool SaveMessageFiles(string conversationId, string messageId, List<BotSharpFile> files) |
| 85 | + { |
| 86 | + if (files.IsNullOrEmpty()) return false; |
| 87 | + |
| 88 | + var dir = GetConversationFileDirectory(conversationId, messageId, createNewDir: true); |
| 89 | + if (!ExistDirectory(dir)) return false; |
| 90 | + |
| 91 | + try |
| 92 | + { |
| 93 | + for (int i = 0; i < files.Count; i++) |
| 94 | + { |
| 95 | + var file = files[i]; |
| 96 | + if (string.IsNullOrEmpty(file.FileData)) |
| 97 | + { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + var (_, bytes) = GetFileInfoFromData(file.FileData); |
| 102 | + var fileType = Path.GetExtension(file.FileName); |
| 103 | + var fileName = $"{i + 1}{fileType}"; |
| 104 | + Thread.Sleep(100); |
| 105 | + File.WriteAllBytes(Path.Combine(dir, fileName), bytes); |
| 106 | + } |
| 107 | + return true; |
| 108 | + } |
| 109 | + catch (Exception ex) |
| 110 | + { |
| 111 | + _logger.LogWarning($"Error when saving conversation files: {ex.Message}"); |
| 112 | + return false; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + public bool DeleteMessageFiles(string conversationId, IEnumerable<string> messageIds, string targetMessageId, string? newMessageId = null) |
| 119 | + { |
| 120 | + if (string.IsNullOrEmpty(conversationId) || messageIds == null) return false; |
| 121 | + |
| 122 | + if (!string.IsNullOrEmpty(targetMessageId) && !string.IsNullOrEmpty(newMessageId)) |
| 123 | + { |
| 124 | + var prevDir = GetConversationFileDirectory(conversationId, targetMessageId); |
| 125 | + var newDir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, newMessageId); |
| 126 | + |
| 127 | + if (ExistDirectory(prevDir)) |
| 128 | + { |
| 129 | + if (ExistDirectory(newDir)) |
| 130 | + { |
| 131 | + Directory.Delete(newDir, true); |
| 132 | + } |
| 133 | + |
| 134 | + Directory.Move(prevDir, newDir); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + foreach (var messageId in messageIds) |
| 139 | + { |
| 140 | + var dir = GetConversationFileDirectory(conversationId, messageId); |
| 141 | + if (string.IsNullOrEmpty(dir)) continue; |
| 142 | + |
| 143 | + Thread.Sleep(100); |
| 144 | + Directory.Delete(dir, true); |
| 145 | + } |
| 146 | + |
| 147 | + return true; |
| 148 | + } |
| 149 | + |
| 150 | + public bool DeleteConversationFiles(IEnumerable<string> conversationIds) |
| 151 | + { |
| 152 | + if (conversationIds.IsNullOrEmpty()) return false; |
| 153 | + |
| 154 | + foreach (var conversationId in conversationIds) |
| 155 | + { |
| 156 | + var convDir = FindConversationDirectory(conversationId); |
| 157 | + if (!ExistDirectory(convDir)) continue; |
| 158 | + |
| 159 | + Directory.Delete(convDir, true); |
| 160 | + } |
| 161 | + return true; |
| 162 | + } |
| 163 | + |
| 164 | + #region Private methods |
| 165 | + private string GetConversationFileDirectory(string? conversationId, string? messageId, bool createNewDir = false) |
| 166 | + { |
| 167 | + if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(messageId)) |
| 168 | + { |
| 169 | + return string.Empty; |
| 170 | + } |
| 171 | + |
| 172 | + var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId); |
| 173 | + if (!Directory.Exists(dir) && createNewDir) |
| 174 | + { |
| 175 | + Directory.CreateDirectory(dir); |
| 176 | + } |
| 177 | + return dir; |
| 178 | + } |
| 179 | + |
| 180 | + private string? FindConversationDirectory(string conversationId) |
| 181 | + { |
| 182 | + if (string.IsNullOrEmpty(conversationId)) return null; |
| 183 | + |
| 184 | + var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId); |
| 185 | + return dir; |
| 186 | + } |
| 187 | + #endregion |
| 188 | +} |
0 commit comments