This repository was archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from noobot/attachments
Receive and Download Files/Attachments
- Loading branch information
Showing
15 changed files
with
575 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/SlackConnector/Connections/Sockets/Messages/Inbound/File.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace SlackConnector.Connections.Sockets.Messages.Inbound | ||
{ | ||
internal class File | ||
{ | ||
public string Id { get; set; } | ||
public int Created { get; set; } | ||
public int Timestamp { get; set; } | ||
public string Name { get; set; } | ||
public string Title { get; set; } | ||
public string Mimetype { get; set; } | ||
public string FileType { get; set; } | ||
|
||
[JsonProperty("pretty_type")] | ||
public string PrettyType { get; set; } | ||
|
||
public string User { get; set; } | ||
public bool Editable { get; set; } | ||
public int Size { get; set; } | ||
public string Mode { get; set; } | ||
|
||
[JsonProperty("is_external")] | ||
public bool IsExternal { get; set; } | ||
|
||
[JsonProperty("external_type")] | ||
public string ExternalType { get; set; } | ||
|
||
[JsonProperty("is_public")] | ||
public bool IsPublic { get; set; } | ||
|
||
[JsonProperty("public_url_shared")] | ||
public bool PublicUrlShared { get; set; } | ||
|
||
[JsonProperty("display_as_bot")] | ||
public bool DisplayAsBot { get; set; } | ||
|
||
public string Username { get; set; } | ||
|
||
[JsonProperty("url_private")] | ||
public string UrlPrivate { get; set; } | ||
|
||
[JsonProperty("url_private_download")] | ||
public string UrlPrivateDownload { get; set; } | ||
|
||
[JsonProperty("thumb_64")] | ||
public string Thumb64 { get; set; } | ||
|
||
[JsonProperty("thumb_80")] | ||
public string Thumb80 { get; set; } | ||
|
||
[JsonProperty("thumb_360")] | ||
public string Thumb360 { get; set; } | ||
|
||
[JsonProperty("thumb_360_w")] | ||
public int Thumb360Width { get; set; } | ||
|
||
[JsonProperty("thumb_360_h")] | ||
public int Thumb360Height { get; set; } | ||
|
||
[JsonProperty("thumb_160")] | ||
public string Thumb160 { get; set; } | ||
|
||
[JsonProperty("thumb_360_gif")] | ||
public string Thumb360Gif { get; set; } | ||
|
||
[JsonProperty("image_exif_rotation")] | ||
public int ImageExifRotation { get; set; } | ||
|
||
[JsonProperty("original_w")] | ||
public int OriginalWidth { get; set; } | ||
|
||
[JsonProperty("original_h")] | ||
public int OriginalHeight { get; set; } | ||
|
||
[JsonProperty("deanimate_gif")] | ||
public string DeanimateGif { get; set; } | ||
|
||
public string Permalink { get; set; } | ||
|
||
[JsonProperty("permalink_public")] | ||
public string PermalinkPublic { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using SlackConnector.Connections.Sockets.Messages.Inbound; | ||
using SlackConnector.Models; | ||
|
||
namespace SlackConnector.Extensions | ||
{ | ||
internal static class FileExtensions | ||
{ | ||
public static SlackFile ToSlackFile(this File file) | ||
{ | ||
if (file == null) | ||
return null; | ||
|
||
return new SlackFile( | ||
file.Id, | ||
file.Created, | ||
file.Timestamp, | ||
file.Name, | ||
file.Title, | ||
file.Mimetype, | ||
file.FileType, | ||
file.PrettyType, | ||
file.User, | ||
file.Editable, | ||
file.Size, | ||
file.Mode, | ||
file.IsExternal, | ||
file.ExternalType, | ||
file.IsPublic, | ||
file.PublicUrlShared, | ||
file.DisplayAsBot, | ||
file.Username, | ||
CreateUri(file.UrlPrivate), | ||
CreateUri(file.UrlPrivateDownload), | ||
file.ImageExifRotation, | ||
file.OriginalWidth, | ||
file.OriginalHeight, | ||
CreateUri(file.DeanimateGif), | ||
CreateUri(file.Permalink), | ||
CreateUri(file.PermalinkPublic), | ||
new SlackThumbnail( | ||
CreateUri(file.Thumb64), | ||
CreateUri(file.Thumb80), | ||
CreateUri(file.Thumb360), | ||
file.Thumb360Width, | ||
file.Thumb360Height, | ||
CreateUri(file.Thumb160), | ||
CreateUri(file.Thumb360Gif) | ||
) | ||
); | ||
} | ||
|
||
private static Uri CreateUri(string url) | ||
{ | ||
return Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out var uri) | ||
? uri | ||
: null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
|
||
namespace SlackConnector.Models | ||
{ | ||
public class SlackFile | ||
{ | ||
public string Id { get; } | ||
public int Created { get; } | ||
public int Timestamp { get; } | ||
public string Name { get; } | ||
public string Title { get; } | ||
public string Mimetype { get; } | ||
public string FileType { get; } | ||
public string PrettyType { get; } | ||
public string User { get; } | ||
public bool Editable { get; } | ||
public int Size { get; } | ||
public string Mode { get; } | ||
public bool IsExternal { get; } | ||
public string ExternalType { get; } | ||
public bool IsPublic { get; } | ||
public bool PublicUrlShared { get; } | ||
public bool DisplayAsBot { get; } | ||
public string Username { get; } | ||
public Uri UrlPrivate { get; } | ||
public Uri UrlPrivateDownload { get; } | ||
public int ImageExifRotation { get; } | ||
public int OriginalWidth { get; } | ||
public int OriginalHeight { get; } | ||
public Uri DeanimateGif { get; } | ||
public Uri Permalink { get; } | ||
public Uri PermalinkPublic { get; } | ||
public SlackThumbnail Thumbnail { get; } | ||
|
||
public SlackFile( | ||
string id, | ||
int created, | ||
int timestamp, | ||
string name, | ||
string title, | ||
string mimetype, | ||
string fileType, | ||
string prettyType, | ||
string user, | ||
bool editable, | ||
int size, | ||
string mode, | ||
bool isExternal, | ||
string externalType, | ||
bool isPublic, | ||
bool publicUrlShared, | ||
bool displayAsBot, | ||
string username, | ||
Uri urlPrivate, | ||
Uri urlPrivateDownload, | ||
int imageExifRotation, | ||
int originalWidth, | ||
int originalHeight, | ||
Uri deanimateGif, | ||
Uri permalink, | ||
Uri permalinkPublic, | ||
SlackThumbnail thumbnail) | ||
{ | ||
Id = id; | ||
Created = created; | ||
Timestamp = timestamp; | ||
Name = name; | ||
Title = title; | ||
Mimetype = mimetype; | ||
FileType = fileType; | ||
PrettyType = prettyType; | ||
User = user; | ||
Editable = editable; | ||
Size = size; | ||
Mode = mode; | ||
IsExternal = isExternal; | ||
ExternalType = externalType; | ||
IsPublic = isPublic; | ||
PublicUrlShared = publicUrlShared; | ||
DisplayAsBot = displayAsBot; | ||
Username = username; | ||
UrlPrivate = urlPrivate; | ||
UrlPrivateDownload = urlPrivateDownload; | ||
ImageExifRotation = imageExifRotation; | ||
OriginalWidth = originalWidth; | ||
OriginalHeight = originalHeight; | ||
DeanimateGif = deanimateGif; | ||
Permalink = permalink; | ||
PermalinkPublic = permalinkPublic; | ||
Thumbnail = thumbnail; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
|
||
namespace SlackConnector.Models | ||
{ | ||
public class SlackThumbnail | ||
{ | ||
public Uri Thumb64 { get; } | ||
public Uri Thumb80 { get; } | ||
public Uri Thumb360 { get; } | ||
public int Thumb360Width { get; } | ||
public int Thumb360Height { get; } | ||
public Uri Thumb160 { get; } | ||
public Uri Thumb360Gif { get; } | ||
|
||
public SlackThumbnail( | ||
Uri thumb64, | ||
Uri thumb80, | ||
Uri thumb360, | ||
int thumb360Width, | ||
int thumb360Height, | ||
Uri thumb160, | ||
Uri thumb360Gif) | ||
{ | ||
Thumb64 = thumb64; | ||
Thumb80 = thumb80; | ||
Thumb360 = thumb360; | ||
Thumb360Width = thumb360Width; | ||
Thumb360Height = thumb360Height; | ||
Thumb160 = thumb160; | ||
Thumb360Gif = thumb360Gif; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
tests/SlackConnector.Tests.Integration/FileDownloadTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Shouldly; | ||
using SixLabors.ImageSharp; | ||
using Xunit; | ||
|
||
namespace SlackConnector.Tests.Integration | ||
{ | ||
public class FileDownloadTests : IntegrationTest | ||
{ | ||
[Fact] | ||
public async Task should_download_file() | ||
{ | ||
// given | ||
var uri = new Uri("https://files.slack.com/files-pri/T3NFBGBAS-FBUSTA0P4/fuuuu.gif"); | ||
|
||
// when | ||
var download = await SlackConnection.DownloadFile(uri); | ||
|
||
// then | ||
using (download) | ||
{ | ||
download.ShouldNotBeNull(); | ||
download.Length.ShouldBeGreaterThan(0); | ||
|
||
var image = Image.Load(download); | ||
image.Width.ShouldBe(43); | ||
image.Height.ShouldBe(29); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.