Skip to content
This repository was archived by the owner on Apr 30, 2024. It is now read-only.

Commit bd12898

Browse files
committed
Added icon converter for attachments
1 parent ea8c228 commit bd12898

File tree

5 files changed

+82
-4
lines changed

5 files changed

+82
-4
lines changed

src/Quarrel.ViewModels/Bindables/Messages/Embeds/BindableAttachment.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Quarrel.Client.Models.Messages.Embeds;
66
using Quarrel.Services.Discord;
77
using Quarrel.Services.Dispatcher;
8+
using System.Text.RegularExpressions;
89

910
namespace Quarrel.Bindables.Messages.Embeds
1011
{
@@ -13,6 +14,8 @@ namespace Quarrel.Bindables.Messages.Embeds
1314
/// </summary>
1415
public class BindableAttachment : BindableItem
1516
{
17+
private const string FileExtensionRegex = @"\.([\w]+)$";
18+
1619
private Attachment _attachment;
1720

1821
internal BindableAttachment(
@@ -33,5 +36,17 @@ public Attachment Attachment
3336
get => _attachment;
3437
private set => SetProperty(ref _attachment, value);
3538
}
39+
40+
/// <summary>
41+
/// Gets the file's extension.
42+
/// </summary>
43+
public string FileExtension
44+
{
45+
get
46+
{
47+
var match = Regex.Match(Attachment.Filename, FileExtensionRegex);
48+
return match.Groups[1].Value;
49+
}
50+
}
3651
}
3752
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Quarrel © 2022
2+
3+
namespace Quarrel.Converters.Discord.Messages.Attachments
4+
{
5+
public class AttachmentIconConverter
6+
{
7+
public static string Convert(string filetype)
8+
{
9+
// TODO: Replace with dictionary from file
10+
return filetype switch
11+
{
12+
// Text file icon
13+
"txt" => "",
14+
15+
// Zipped file(s) icon
16+
"7z" or
17+
"gz" or
18+
"zip" => "",
19+
20+
// Image icon
21+
"png" or
22+
"jpg" or
23+
"jpeg" or
24+
"bmp" or
25+
"gif" => "",
26+
27+
// Audio icon
28+
"mp3" or
29+
"wav" => "",
30+
31+
// Video icon
32+
"mp4" or
33+
"mov" => "",
34+
35+
// 3D icon
36+
"obj" or
37+
"blend" => "",
38+
39+
// Console icon
40+
"ps1" or
41+
"batch" => "",
42+
43+
// App Package icon
44+
"appx" or
45+
"appxbundle" or
46+
"msix" or
47+
"msixbundle" or
48+
"appinstaller" => "",
49+
50+
// Certificate icon
51+
"cer" => "",
52+
53+
// Code icon
54+
"json" => "",
55+
56+
// Disc icon
57+
"iso" => "",
58+
59+
// File icon
60+
_ => "",
61+
};
62+
}
63+
}
64+
}

src/Quarrel/DataTemplates/MessageTemplates.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<ColumnDefinition Width="Auto"/>
2727
<ColumnDefinition Width="Auto"/>
2828
</Grid.ColumnDefinitions>
29-
<FontIcon x:Name="FileIcon" FontSize="36" Glyph="" HorizontalAlignment="Left" Margin="0,0,6,0"/>
29+
<FontIcon x:Name="FileIcon" FontSize="36" Glyph="{x:Bind aconvert:AttachmentIconConverter.Convert(FileExtension)}" HorizontalAlignment="Left" Margin="0,0,6,0"/>
3030
<StackPanel Grid.Column="1">
3131
<HyperlinkButton Content="{x:Bind Attachment.Filename}" NavigateUri="{x:Bind Attachment.ProxyUrl}" FontSize="18" Padding="0" Style="{StaticResource PlainTextHyperlinkStyle}"/>
3232
<TextBlock Text="{x:Bind aconvert:HumanizeByteSizeConverter.Convert(Attachment.Size)}" FontSize="11" Opacity="0.5" Margin="0,-2,0,0"/>

src/Quarrel/Quarrel.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
<Compile Include="Converters\Discord\Channels\IsPrivateChannelConverter.cs" />
150150
<Compile Include="Converters\Discord\Channels\IsPrivateChannelVisibleConverter.cs" />
151151
<Compile Include="Converters\Discord\Channels\MemberCountConverter.cs" />
152+
<Compile Include="Converters\Discord\Messages\Attachments\AttachmentIconConverter.cs" />
152153
<Compile Include="Converters\Discord\Messages\Attachments\HumanizeByteSizeConverter.cs" />
153154
<Compile Include="Converters\Discord\Messages\InfoMessageColorConverter.cs" />
154155
<Compile Include="Converters\Discord\Messages\InfoMessageContentConverter.cs" />

src/Quarrel/Selectors/Messages/AttachmentTemplateSelector.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ protected override DataTemplate SelectTemplateCore(object item, DependencyObject
1616
{
1717
if (item is BindableAttachment attachment)
1818
{
19-
int index = attachment.Attachment.Filename.LastIndexOf('.');
20-
string filetype = attachment.Attachment.Filename.Substring(index + 1).ToLowerInvariant();
21-
return filetype switch
19+
return attachment.FileExtension switch
2220
{
2321
"png" or
2422
"jpg" or

0 commit comments

Comments
 (0)