1
- using Microsoft . AspNetCore . StaticFiles ;
1
+ using BotSharp . Abstraction . Browsing ;
2
+ using Microsoft . EntityFrameworkCore ;
2
3
using System . IO ;
4
+ using System . Linq ;
3
5
using System . Threading ;
4
6
5
7
namespace BotSharp . Core . Files ;
6
8
7
9
public partial class BotSharpFileService
8
10
{
9
- public IEnumerable < MessageFileModel > GetChatImages ( string conversationId , List < RoleDialogModel > conversations , int offset = 1 )
11
+ public async Task < IEnumerable < MessageFileModel > > GetChatImages ( string conversationId , string source , IEnumerable < string > fileTypes ,
12
+ List < RoleDialogModel > conversations , int ? offset = null )
10
13
{
11
14
var files = new List < MessageFileModel > ( ) ;
12
15
if ( string . IsNullOrEmpty ( conversationId ) || conversations . IsNullOrEmpty ( ) )
13
16
{
14
- return files ;
17
+ return new List < MessageFileModel > ( ) ;
15
18
}
16
19
17
20
if ( offset <= 0 )
@@ -23,55 +26,161 @@ public IEnumerable<MessageFileModel> GetChatImages(string conversationId, List<R
23
26
offset = MAX_OFFSET ;
24
27
}
25
28
26
- var messageIds = conversations . Select ( x => x . MessageId ) . Distinct ( ) . TakeLast ( offset ) . ToList ( ) ;
27
- files = GetMessageFiles ( conversationId , messageIds , imageOnly : true ) . ToList ( ) ;
29
+ var messageIds = new List < string > ( ) ;
30
+ if ( offset . HasValue )
31
+ {
32
+ messageIds = conversations . Select ( x => x . MessageId ) . Distinct ( ) . TakeLast ( offset . Value ) . ToList ( ) ;
33
+ }
34
+ else
35
+ {
36
+ messageIds = conversations . Select ( x => x . MessageId ) . Distinct ( ) . ToList ( ) ;
37
+ }
38
+
39
+ files = await GetMessageFiles ( conversationId , messageIds , source , fileTypes ) ;
40
+ return files ;
41
+ }
42
+
43
+ private async Task < List < MessageFileModel > > GetMessageFiles ( string conversationId , IEnumerable < string > messageIds , string source , IEnumerable < string > fileTypes )
44
+ {
45
+ var files = new List < MessageFileModel > ( ) ;
46
+ if ( string . IsNullOrEmpty ( conversationId ) || messageIds . IsNullOrEmpty ( ) || fileTypes . IsNullOrEmpty ( ) ) return files ;
47
+
48
+ var isNeedScreenShot = fileTypes . Any ( x => _allowScreenShotTypes . Contains ( x ) ) ;
49
+ var onlyScreenShot = fileTypes . All ( x => _allowScreenShotTypes . Contains ( x ) ) ;
50
+
51
+ try
52
+ {
53
+ var contextId = string . Empty ;
54
+ var web = _services . GetRequiredService < IWebBrowser > ( ) ;
55
+ var preFixPath = Path . Combine ( _baseDir , CONVERSATION_FOLDER , conversationId , FILE_FOLDER ) ;
56
+
57
+ if ( isNeedScreenShot )
58
+ {
59
+ contextId = Guid . NewGuid ( ) . ToString ( ) ;
60
+ await web . LaunchBrowser ( contextId , string . Empty ) ;
61
+ }
62
+
63
+ foreach ( var messageId in messageIds )
64
+ {
65
+ var dir = Path . Combine ( preFixPath , messageId , source ) ;
66
+ if ( ! ExistDirectory ( dir ) ) continue ;
67
+
68
+ foreach ( var subDir in Directory . GetDirectories ( dir ) )
69
+ {
70
+ var file = Directory . GetFiles ( subDir ) . FirstOrDefault ( ) ;
71
+ if ( file == null ) continue ;
72
+
73
+ var index = subDir . Split ( Path . DirectorySeparatorChar ) . Last ( ) ;
74
+ var contentType = GetFileContentType ( file ) ;
75
+
76
+ if ( ( ! isNeedScreenShot || ( isNeedScreenShot && ! onlyScreenShot ) ) && _allowedImageTypes . Contains ( contentType ) )
77
+ {
78
+ var model = new MessageFileModel ( )
79
+ {
80
+ MessageId = messageId ,
81
+ FileStorageUrl = file ,
82
+ ContentType = contentType
83
+ } ;
84
+ files . Add ( model ) ;
85
+ }
86
+ else if ( ( isNeedScreenShot && ! onlyScreenShot || onlyScreenShot ) && ! _allowedImageTypes . Contains ( contentType ) )
87
+ {
88
+ var screenShotDir = Path . Combine ( subDir , SCREENSHOT_FILE_FOLDER ) ;
89
+ if ( ExistDirectory ( screenShotDir ) && Directory . GetFiles ( screenShotDir ) . Any ( ) )
90
+ {
91
+ file = Directory . GetFiles ( screenShotDir ) . First ( ) ;
92
+ contentType = GetFileContentType ( file ) ;
93
+
94
+ var model = new MessageFileModel ( )
95
+ {
96
+ MessageId = messageId ,
97
+ FileStorageUrl = file ,
98
+ ContentType = contentType
99
+ } ;
100
+ files . Add ( model ) ;
101
+ }
102
+ else
103
+ {
104
+ await web . GoToPage ( contextId , file ) ;
105
+ var path = Path . Combine ( subDir , SCREENSHOT_FILE_FOLDER , $ "{ Guid . NewGuid ( ) } .png") ;
106
+ await web . ScreenshotAsync ( contextId , path ) ;
107
+ contentType = GetFileContentType ( path ) ;
108
+
109
+ var model = new MessageFileModel ( )
110
+ {
111
+ MessageId = messageId ,
112
+ FileStorageUrl = path ,
113
+ ContentType = contentType
114
+ } ;
115
+ files . Add ( model ) ;
116
+ }
117
+ }
118
+ }
119
+ }
120
+
121
+ if ( isNeedScreenShot )
122
+ {
123
+ await web . CloseBrowser ( contextId ) ;
124
+ }
125
+ }
126
+ catch ( Exception ex )
127
+ {
128
+ _logger . LogWarning ( $ "Error when reading conversation ({ conversationId } ) files: { ex . Message } ") ;
129
+ }
130
+
28
131
return files ;
29
132
}
30
133
31
- public IEnumerable < MessageFileModel > GetMessageFiles ( string conversationId , IEnumerable < string > messageIds , bool imageOnly = false )
134
+ public IEnumerable < MessageFileModel > GetMessageFiles ( string conversationId , IEnumerable < string > messageIds ,
135
+ string source , bool imageOnly = false )
32
136
{
33
137
var files = new List < MessageFileModel > ( ) ;
34
138
if ( messageIds . IsNullOrEmpty ( ) ) return files ;
35
139
36
140
foreach ( var messageId in messageIds )
37
141
{
38
- var dir = GetConversationFileDirectory ( conversationId , messageId ) ;
142
+ var dir = Path . Combine ( _baseDir , CONVERSATION_FOLDER , conversationId , FILE_FOLDER , messageId , source ) ;
39
143
if ( ! ExistDirectory ( dir ) )
40
144
{
41
145
continue ;
42
146
}
43
147
44
- foreach ( var file in Directory . GetFiles ( dir ) )
148
+ foreach ( var subDir in Directory . GetDirectories ( dir ) )
45
149
{
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 ) ;
150
+ var index = subDir . Split ( Path . DirectorySeparatorChar ) . Last ( ) ;
55
151
56
- var model = new MessageFileModel ( )
152
+ foreach ( var file in Directory . GetFiles ( subDir ) )
57
153
{
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 ) ;
154
+ var contentType = GetFileContentType ( file ) ;
155
+ if ( imageOnly && ! _allowedImageTypes . Contains ( contentType ) )
156
+ {
157
+ continue ;
158
+ }
159
+
160
+ var fileName = Path . GetFileNameWithoutExtension ( file ) ;
161
+ var extension = Path . GetExtension ( file ) ;
162
+ var fileType = extension . Substring ( 1 ) ;
163
+
164
+ var model = new MessageFileModel ( )
165
+ {
166
+ MessageId = messageId ,
167
+ FileUrl = $ "/conversation/{ conversationId } /message/{ messageId } /{ source } /file/{ index } /{ fileName } ",
168
+ FileStorageUrl = file ,
169
+ FileName = fileName ,
170
+ FileType = fileType ,
171
+ ContentType = contentType
172
+ } ;
173
+ files . Add ( model ) ;
174
+ }
66
175
}
67
176
}
68
177
69
178
return files ;
70
179
}
71
180
72
- public string GetMessageFile ( string conversationId , string messageId , string fileName )
181
+ public string GetMessageFile ( string conversationId , string messageId , string source , string index , string fileName )
73
182
{
74
- var dir = GetConversationFileDirectory ( conversationId , messageId ) ;
183
+ var dir = Path . Combine ( _baseDir , CONVERSATION_FOLDER , conversationId , FILE_FOLDER , messageId , source , index ) ;
75
184
if ( ! ExistDirectory ( dir ) )
76
185
{
77
186
return string . Empty ;
@@ -81,7 +190,17 @@ public string GetMessageFile(string conversationId, string messageId, string fil
81
190
return found ;
82
191
}
83
192
84
- public bool SaveMessageFiles ( string conversationId , string messageId , List < BotSharpFile > files )
193
+ public bool HasConversationUserFiles ( string conversationId )
194
+ {
195
+ if ( string . IsNullOrEmpty ( conversationId ) ) return false ;
196
+
197
+ var dir = Path . Combine ( _baseDir , CONVERSATION_FOLDER , conversationId , FILE_FOLDER ) ;
198
+ if ( ! ExistDirectory ( dir ) ) return false ;
199
+
200
+ return Directory . GetDirectories ( dir ) . Any ( ) ;
201
+ }
202
+
203
+ public bool SaveMessageFiles ( string conversationId , string messageId , string source , List < BotSharpFile > files )
85
204
{
86
205
if ( files . IsNullOrEmpty ( ) ) return false ;
87
206
@@ -99,11 +218,16 @@ public bool SaveMessageFiles(string conversationId, string messageId, List<BotSh
99
218
}
100
219
101
220
var ( _, bytes ) = GetFileInfoFromData ( file . FileData ) ;
102
- var fileType = Path . GetExtension ( file . FileName ) ;
103
- var fileName = $ "{ i + 1 } { fileType } ";
104
221
Thread . Sleep ( 100 ) ;
105
- File . WriteAllBytes ( Path . Combine ( dir , fileName ) , bytes ) ;
222
+ var subDir = Path . Combine ( dir , source , $ "{ i + 1 } ") ;
223
+ if ( ! ExistDirectory ( subDir ) )
224
+ {
225
+ Directory . CreateDirectory ( subDir ) ;
226
+ }
227
+
228
+ File . WriteAllBytes ( Path . Combine ( subDir , file . FileName ) , bytes ) ;
106
229
}
230
+
107
231
return true ;
108
232
}
109
233
catch ( Exception ex )
@@ -114,7 +238,6 @@ public bool SaveMessageFiles(string conversationId, string messageId, List<BotSh
114
238
}
115
239
116
240
117
-
118
241
public bool DeleteMessageFiles ( string conversationId , IEnumerable < string > messageIds , string targetMessageId , string ? newMessageId = null )
119
242
{
120
243
if ( string . IsNullOrEmpty ( conversationId ) || messageIds == null ) return false ;
@@ -132,6 +255,13 @@ public bool DeleteMessageFiles(string conversationId, IEnumerable<string> messag
132
255
}
133
256
134
257
Directory . Move ( prevDir , newDir ) ;
258
+ Thread . Sleep ( 100 ) ;
259
+
260
+ var botDir = Path . Combine ( newDir , BOT_FILE_FOLDER ) ;
261
+ if ( ExistDirectory ( botDir ) )
262
+ {
263
+ Directory . Delete ( botDir , true ) ;
264
+ }
135
265
}
136
266
}
137
267
0 commit comments