-
Notifications
You must be signed in to change notification settings - Fork 319
Expand file tree
/
Copy pathAttachmentProcessor.cs
More file actions
178 lines (153 loc) · 6.24 KB
/
AttachmentProcessor.cs
File metadata and controls
178 lines (153 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) Microsoft Corporation. All rights reserved.
using Microsoft.Extensions.AI;
using Microsoft.Windows.Search.AppContentIndex;
using Notes.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
namespace Notes
{
public static class AttachmentProcessor
{
internal static EventHandler<AttachmentProcessedEventArgs>? AttachmentProcessed;
private readonly static List<Attachment> _toBeProcessed = new();
private static bool _isProcessing = false;
public async static Task AddAttachment(Attachment attachment)
{
_toBeProcessed.Add(attachment);
if (!_isProcessing)
{
try
{
_isProcessing = true;
await Process();
}
catch (Exception ex)
{
Debug.WriteLine($"Error processing attachment: {ex.Message}");
}
_isProcessing = false;
}
}
public async static Task RemoveAttachment(Attachment attachment)
{
if (MainWindow.AppContentIndexer != null)
{
await Task.Run(() =>
{
MainWindow.AppContentIndexer.Remove(attachment.Id.ToString());
});
Debug.WriteLine($"Deleted image from index: {attachment.Filename}");
}
else
{
Debug.WriteLine("AppContentIndexer is null");
}
}
private static async Task Process()
{
while (_toBeProcessed.Count > 0)
{
var attachment = _toBeProcessed[0];
_toBeProcessed.RemoveAt(0);
if (attachment.IsProcessed)
{
continue;
}
if (attachment.Type == NoteAttachmentType.Image)
{
await ProcessImage(attachment);
}
else if (attachment.Type == NoteAttachmentType.Audio || attachment.Type == NoteAttachmentType.Video)
{
throw new NotSupportedException("audio and video files are not supported");
}
}
}
public static async Task ReIndexImage(Attachment attachment)
{
// get softwarebitmap from file
var attachmentsFolder = await Utils.GetAttachmentsFolderAsync();
var file = await attachmentsFolder.GetFileAsync(attachment.Filename);
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
IndexableAppContent imageContent = AppManagedIndexableAppContent.CreateFromBitmap(attachment.Id.ToString(), softwareBitmap);
Debug.WriteLine($"Indexing image {attachment.Filename}");
if (MainWindow.AppContentIndexer != null)
{
await Task.Run(() =>
{
MainWindow.AppContentIndexer.AddOrUpdate(imageContent);
});
attachment.IsProcessed = true;
Debug.WriteLine($"Indexed image {attachment.Filename}");
}
else
{
Debug.WriteLine("appContentIndexer is null");
}
}
}
private static async Task ProcessImage(Models.Attachment attachment, EventHandler<float>? progress = null)
{
// get softwarebitmap from file
var attachmentsFolder = await Utils.GetAttachmentsFolderAsync();
var file = await attachmentsFolder.GetFileAsync(attachment.Filename);
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
IndexableAppContent imageContent = AppManagedIndexableAppContent.CreateFromBitmap(attachment.Id.ToString(), softwareBitmap);
Debug.WriteLine($"Indexing image {attachment.Filename}");
if (MainWindow.AppContentIndexer != null)
{
await Task.Run(() =>
{
MainWindow.AppContentIndexer.AddOrUpdate(imageContent);
});
attachment.IsProcessed = true;
Debug.WriteLine($"Indexed image {attachment.Filename}");
}
else
{
Debug.WriteLine("appContentIndexer is null");
}
InvokeAttachmentProcessedComplete(attachment);
var context = await AppDataContext.GetCurrentAsync();
context.Update(attachment);
await context.SaveChangesAsync();
}
}
private async static Task<string> SaveTextToFileAsync(string text, string filename)
{
var stateAttachmentsFolder = await Utils.GetAttachmentsTranscriptsFolderAsync();
var file = await stateAttachmentsFolder.CreateFileAsync(filename, CreationCollisionOption.GenerateUniqueName);
await FileIO.WriteTextAsync(file, text);
return file.Name;
}
private static void InvokeAttachmentProcessedComplete(Attachment attachment)
{
if (AttachmentProcessed != null)
{
AttachmentProcessed.Invoke(null, new AttachmentProcessedEventArgs
{
AttachmentId = attachment.Id,
Progress = 1,
ProcessingStep = "Complete"
});
}
}
}
public class AttachmentProcessedEventArgs
{
public int AttachmentId { get; set; }
public float Progress { get; set; }
public string? ProcessingStep { get; set; }
}
}