Skip to content

Commit

Permalink
fix: make internal stuff internal + sealed
Browse files Browse the repository at this point in the history
feat: gcp maybe
  • Loading branch information
Lulalaby committed Jul 7, 2023
1 parent 5240875 commit 92e03ed
Show file tree
Hide file tree
Showing 20 changed files with 252 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;

Expand Down
11 changes: 11 additions & 0 deletions DisCatSharp/Entities/Channel/DiscordChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,17 @@ public async Task UpdateCurrentUserVoiceStateAsync(bool? suppress, DateTimeOffse
await this.Discord.ApiClient.UpdateCurrentUserVoiceStateAsync(this.GuildId.Value, this.Id, suppress, requestToSpeakTimestamp).ConfigureAwait(false);
}

public async Task<GcpAttachmentUploadInformation> UploadFileAsync(string name, Stream stream, string? description = null)
{
GcpAttachment attachment = new(name, stream);
var response = await this.Discord.ApiClient.RequestFileUploadAsync(this.Id, attachment);
var target = response.Attachments.First();
_ = Task.Run(() => this.Discord.ApiClient.UploadGcpFile(target, stream));
target.Filename = name;
target.Description = description;
return target;
}

/// <summary>
/// Calculates permissions for a given member.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public DiscordFollowupMessageBuilder AddFile(string filename, Stream data, bool
if (this.Files.Count >= 10)
throw new ArgumentException("Cannot send more than 10 files with a single message.");

if (this._files.Any(x => x.FileName == filename))
if (this._files.Any(x => x.Filename == filename))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
Expand All @@ -249,7 +249,7 @@ public DiscordFollowupMessageBuilder AddFile(FileStream stream, bool resetStream
if (this.Files.Count >= 10)
throw new ArgumentException("Cannot send more than 10 files with a single message.");

if (this._files.Any(x => x.FileName == stream.Name))
if (this._files.Any(x => x.Filename == stream.Name))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
Expand All @@ -273,7 +273,7 @@ public DiscordFollowupMessageBuilder AddFiles(Dictionary<string, Stream> files,

foreach (var file in files)
{
if (this._files.Any(x => x.FileName == file.Key))
if (this._files.Any(x => x.Filename == file.Key))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public DiscordInteractionResponseBuilder AddFile(string filename, Stream data, b
if (this.Files.Count >= 10)
throw new ArgumentException("Cannot send more than 10 files with a single message.");

if (this._files.Any(x => x.FileName == filename))
if (this._files.Any(x => x.Filename == filename))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
Expand All @@ -304,7 +304,7 @@ public DiscordInteractionResponseBuilder AddFile(FileStream stream, bool resetSt
if (this.Files.Count >= 10)
throw new ArgumentException("Cannot send more than 10 files with a single message.");

if (this._files.Any(x => x.FileName == stream.Name))
if (this._files.Any(x => x.Filename == stream.Name))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
Expand All @@ -328,7 +328,7 @@ public DiscordInteractionResponseBuilder AddFiles(Dictionary<string, Stream> fil

foreach (var file in files)
{
if (this._files.Any(x => x.FileName == file.Key))
if (this._files.Any(x => x.Filename == file.Key))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
Expand Down
23 changes: 20 additions & 3 deletions DisCatSharp/Entities/Message/DiscordAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using DisCatSharp.Attributes;
using DisCatSharp.Enums;

using Newtonsoft.Json;
Expand All @@ -29,19 +30,26 @@ namespace DisCatSharp.Entities;
/// <summary>
/// Represents an attachment for a message.
/// </summary>
public class DiscordAttachment : SnowflakeObject
public class DiscordAttachment : NullableSnowflakeObject
{
/// <summary>
/// Gets the name of the file.
/// </summary>
[JsonProperty("filename", NullValueHandling = NullValueHandling.Ignore)]
public string FileName { get; internal set; }
public string Filename { get; internal set; }

/// <summary>
/// Gets the name of the file. Please use <see cref="Filename"/> in future.
/// </summary>
[Deprecated("Naming was incorrect, will be removed in future in favor of Filename")]
public string FileName
=> this.Filename;

/// <summary>
/// Gets the description of the file.
/// </summary>
[JsonProperty("description", NullValueHandling = NullValueHandling.Ignore)]
public string Description { get; set; }
public string? Description { get; set; }

/// <summary>
/// Gets the media, or MIME, type of the file.
Expand Down Expand Up @@ -79,6 +87,12 @@ public class DiscordAttachment : SnowflakeObject
[JsonProperty("width", NullValueHandling = NullValueHandling.Ignore)]
public int? Width { get; internal set; }

/// <summary>
/// Gets the uploaded filename if the attachment was uploaded via GCP.
/// </summary>
[JsonProperty("uploaded_filename", NullValueHandling = NullValueHandling.Ignore)]
internal string? UploadedFilename { get; set; }

/// <summary>
/// Gets whether this attachment is ephemeral.
/// Ephemeral attachments will automatically be removed after a set period of time.
Expand All @@ -101,6 +115,9 @@ public class DiscordAttachment : SnowflakeObject
[JsonProperty("waveform", NullValueHandling = NullValueHandling.Ignore)]
public string WaveForm { get; internal set; }

/// <summary>
/// Gets the attachment flags.
/// </summary>
[JsonProperty("flags", NullValueHandling = NullValueHandling.Ignore)]
public AttachmentFlags Flags { get; internal set; } = AttachmentFlags.None;

Expand Down
20 changes: 17 additions & 3 deletions DisCatSharp/Entities/Message/DiscordMessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
using System.Linq;
using System.Threading.Tasks;

using static System.Net.WebRequestMethods;

namespace DisCatSharp.Entities;

/// <summary>
Expand Down Expand Up @@ -292,7 +294,7 @@ public DiscordMessageBuilder WithFile(string fileName, Stream stream, bool reset
if (this.Files.Count > 10)
throw new ArgumentException("Cannot send more than 10 files with a single message.");

if (this.FilesInternal.Any(x => x.FileName == fileName))
if (this.FilesInternal.Any(x => x.Filename == fileName))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
Expand All @@ -315,7 +317,7 @@ public DiscordMessageBuilder WithFile(FileStream stream, bool resetStreamPositio
if (this.Files.Count > 10)
throw new ArgumentException("Cannot send more than 10 files with a single message.");

if (this.FilesInternal.Any(x => x.FileName == stream.Name))
if (this.FilesInternal.Any(x => x.Filename == stream.Name))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
Expand All @@ -339,7 +341,7 @@ public DiscordMessageBuilder WithFiles(Dictionary<string, Stream> files, bool re

foreach (var file in files)
{
if (this.FilesInternal.Any(x => x.FileName == file.Key))
if (this.FilesInternal.Any(x => x.Filename == file.Key))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
Expand All @@ -351,6 +353,18 @@ public DiscordMessageBuilder WithFiles(Dictionary<string, Stream> files, bool re
return this;
}

public DiscordMessageBuilder AddGcpAttachment(GcpAttachmentUploadInformation gcpAttachment)
{
this.AttachmentsInternal.Add(new()
{
Filename = gcpAttachment.Filename,
UploadedFilename = gcpAttachment.UploadFilename,
Description = gcpAttachment.Description
});

return this;
}

/// <summary>
/// Modifies the given attachments on edit.
/// </summary>
Expand Down
19 changes: 14 additions & 5 deletions DisCatSharp/Entities/Message/DiscordMessageFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

using System.IO;

using DisCatSharp.Attributes;

namespace DisCatSharp.Entities;

/// <summary>
Expand All @@ -32,15 +34,15 @@ public class DiscordMessageFile
/// <summary>
/// Initializes a new instance of the <see cref="DiscordMessageFile"/> class.
/// </summary>
/// <param name="fileName">The file name.</param>
/// <param name="filename">The file name.</param>
/// <param name="stream">The stream.</param>
/// <param name="resetPositionTo">The reset position to.</param>
/// <param name="fileType">The file type.</param>
/// <param name="contentType">The content type.</param>
/// <param name="description">The description.</param>
internal DiscordMessageFile(string fileName, Stream stream, long? resetPositionTo, string fileType = null, string contentType = null, string description = null)
internal DiscordMessageFile(string filename, Stream stream, long? resetPositionTo, string fileType = null, string contentType = null, string description = null)
{
this.FileName = fileName;
this.Filename = filename;
this.FileType = fileType;
this.ContentType = contentType;
this.Stream = stream;
Expand All @@ -49,9 +51,16 @@ internal DiscordMessageFile(string fileName, Stream stream, long? resetPositionT
}

/// <summary>
/// Gets the FileName of the File.
/// Gets the name of the File.
/// </summary>
public string Filename { get; internal set; }

/// <summary>
/// Gets the FileName of the File. Please use <see cref="Filename"/> in future.
/// </summary>
public string FileName { get; internal set; }
[Deprecated("Naming was incorrect, will be removed in future in favor of Filename")]
public string FileName
=> this.Filename;

/// <summary>
/// Gets the description of the File.
Expand Down
75 changes: 75 additions & 0 deletions DisCatSharp/Entities/Message/GcpAttachment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// This file is part of the DisCatSharp project, based off DSharpPlus.
//
// Copyright (c) 2021-2023 AITSYS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Collections.Generic;
using System.IO;

using DisCatSharp.Enums;

using Newtonsoft.Json;

namespace DisCatSharp.Entities;

public sealed class GcpAttachment : ObservableApiObject
{
[JsonProperty("file_size")]
public int FileSize { get; internal set; }

[JsonProperty("filename")]
public string Filename { get; internal set; }

internal GcpAttachment(string fileName, int fileSize)
{
this.Filename = fileName;
this.FileSize = fileSize;
}

internal GcpAttachment(string filename, Stream file)
{
this.Filename = filename;
this.FileSize = int.TryParse(file.Length.ToString(), out var size)
? size
: throw new FileLoadException("File size too big", filename);
}
}

public sealed class GcpAttachmentsResponse : ObservableApiObject
{
[JsonProperty("attachments")]
public List<GcpAttachmentUploadInformation> Attachments { get; set; } = new();
}

public sealed class GcpAttachmentUploadInformation : NullableSnowflakeObject
{
[JsonProperty("upload_url")]
internal Uri UploadUrl { get; set; }

[JsonProperty("upload_filename")]
public string UploadFilename { get; internal set; }

[JsonIgnore]
public string Filename { get; internal set; }

[JsonIgnore]
public string? Description { get; internal set; }
}
6 changes: 3 additions & 3 deletions DisCatSharp/Entities/Webhook/DiscordWebhookBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public DiscordWebhookBuilder AddFile(string filename, Stream data, bool resetStr
if (this.Files.Count > 10)
throw new ArgumentException("Cannot send more than 10 files with a single message.");

if (this._files.Any(x => x.FileName == filename))
if (this._files.Any(x => x.Filename == filename))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
Expand All @@ -306,7 +306,7 @@ public DiscordWebhookBuilder AddFile(FileStream stream, bool resetStreamPosition
if (this.Files.Count > 10)
throw new ArgumentException("Cannot send more than 10 files with a single message.");

if (this._files.Any(x => x.FileName == stream.Name))
if (this._files.Any(x => x.Filename == stream.Name))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
Expand All @@ -329,7 +329,7 @@ public DiscordWebhookBuilder AddFiles(Dictionary<string, Stream> files, bool res

foreach (var file in files)
{
if (this._files.Any(x => x.FileName == file.Key))
if (this._files.Any(x => x.Filename == file.Key))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace DisCatSharp.Net.Abstractions;
/// <summary>
/// Represents a application command create payload.
/// </summary>
internal class RestApplicationCommandCreatePayload : ObservableApiObject
internal sealed class RestApplicationCommandCreatePayload : ObservableApiObject
{
/// <summary>
/// Gets the type.
Expand Down Expand Up @@ -98,7 +98,7 @@ internal class RestApplicationCommandCreatePayload : ObservableApiObject
/// <summary>
/// Represents a application command edit payload.
/// </summary>
internal class RestApplicationCommandEditPayload : ObservableApiObject
internal sealed class RestApplicationCommandEditPayload : ObservableApiObject
{
/// <summary>
/// Gets the name.
Expand Down Expand Up @@ -152,7 +152,7 @@ internal class RestApplicationCommandEditPayload : ObservableApiObject
/// <summary>
/// Represents a interaction response payload.
/// </summary>
internal class RestInteractionResponsePayload : ObservableApiObject
internal sealed class RestInteractionResponsePayload : ObservableApiObject
{
/// <summary>
/// Gets the type.
Expand All @@ -176,7 +176,7 @@ internal class RestInteractionResponsePayload : ObservableApiObject
/// <summary>
/// Represents a interaction response payload.
/// </summary>
internal class RestInteractionModalResponsePayload : ObservableApiObject
internal sealed class RestInteractionModalResponsePayload : ObservableApiObject
{
/// <summary>
/// Gets the type.
Expand All @@ -194,7 +194,7 @@ internal class RestInteractionModalResponsePayload : ObservableApiObject
/// <summary>
/// Represents a followup message create payload.
/// </summary>
internal class RestFollowupMessageCreatePayload : ObservableApiObject
internal sealed class RestFollowupMessageCreatePayload : ObservableApiObject
{
/// <summary>
/// Gets the content.
Expand Down Expand Up @@ -242,7 +242,7 @@ internal class RestFollowupMessageCreatePayload : ObservableApiObject
/// <summary>
/// Represents a role connection metadata payload.
/// </summary>
internal class RestApplicationRoleConnectionMetadataPayload : ObservableApiObject
internal sealed class RestApplicationRoleConnectionMetadataPayload : ObservableApiObject
{
/// <summary>
/// Gets the metadata type.
Expand Down
Loading

0 comments on commit 92e03ed

Please sign in to comment.