Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #77 from noobot/attachments
Browse files Browse the repository at this point in the history
Receive and Download Files/Attachments
  • Loading branch information
Workshop2 authored Jul 24, 2018
2 parents b554b6c + dac52e5 commit 6a20979
Show file tree
Hide file tree
Showing 15 changed files with 575 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public ChatMessage()
public string User { get; set; }
public string Text { get; set; }
public string Team { get; set; }
public File File { get; set; }

[JsonProperty("ts")]
public double Timestamp { get; set; }
Expand Down
84 changes: 84 additions & 0 deletions src/SlackConnector/Connections/Sockets/Messages/Inbound/File.cs
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; }
}
}
60 changes: 60 additions & 0 deletions src/SlackConnector/Extensions/FileExtensions.cs
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;
}
}
}
5 changes: 5 additions & 0 deletions src/SlackConnector/ISlackConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public interface ISlackConnection
/// <returns></returns>
Task Ping();

/// <summary>
/// Downloads a file from slack. Best used in conjunction with the file class found in SlackMessage
/// </summary>
Task<Stream> DownloadFile(Uri downloadUri);

/// <summary>
/// Raised when the websocket disconnects from the mothership.
/// </summary>
Expand Down
93 changes: 93 additions & 0 deletions src/SlackConnector/Models/SlackFile.cs
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;
}
}
}
1 change: 1 addition & 0 deletions src/SlackConnector/Models/SlackMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public class SlackMessage
public SlackUser User { get; set; }
public double Timestamp { get; set; }
public SlackMessageSubType MessageSubType { get; set; }
public SlackFile File { get; set; }
}
}
33 changes: 33 additions & 0 deletions src/SlackConnector/Models/SlackThumbnail.cs
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;
}
}
}
17 changes: 16 additions & 1 deletion src/SlackConnector/SlackConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Flurl.Http;
using SlackConnector.BotHelpers;
using SlackConnector.Connections;
using SlackConnector.Connections.Clients.Channel;
Expand Down Expand Up @@ -123,7 +124,8 @@ private Task HandleMessage(ChatMessage inboundMessage)
ChatHub = GetChatHub(inboundMessage.Channel),
RawData = inboundMessage.RawData,
MentionsBot = _mentionDetector.WasBotMentioned(Self.Name, Self.Id, inboundMessage.Text),
MessageSubType = inboundMessage.MessageSubType.ToSlackMessageSubType()
MessageSubType = inboundMessage.MessageSubType.ToSlackMessageSubType(),
File = inboundMessage.File.ToSlackFile()
};

return RaiseMessageReceived(message);
Expand Down Expand Up @@ -453,6 +455,19 @@ public async Task Ping()
await _webSocketClient.SendMessage(new PingMessage());
}

public Task<Stream> DownloadFile(Uri downloadUri)
{
if (!downloadUri.Host.Equals("files.slack.com"))
{
throw new ArgumentException("Invalid uri. Should be targetting files.slack.com", nameof(downloadUri));
}

return downloadUri.AbsoluteUri
.WithOAuthBearerToken(SlackKey)
.AllowHttpStatus()
.GetStreamAsync();
}

public event DisconnectEventHandler OnDisconnect;
private void RaiseOnDisconnect()
{
Expand Down
32 changes: 32 additions & 0 deletions tests/SlackConnector.Tests.Integration/FileDownloadTests.cs
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Shouldly" Version="3.0.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0004" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task should_connect_and_stuff()

// then
SlackConnection.IsConnected.ShouldBeTrue();
//Thread.Sleep(TimeSpan.FromMinutes(1));
//Thread.Sleep(TimeSpan.FromMinutes(5));

// when
await SlackConnection.Close();
Expand Down
Loading

0 comments on commit 6a20979

Please sign in to comment.