-
Notifications
You must be signed in to change notification settings - Fork 73
feat: add file type filtering for attachment options and file uploads #572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+58
−0
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9b57a20
feat: add file type filtering for attachment options and file uploads
vmphase 9b61188
fix(file_type): disallow trailing dot or dash in file type extensions
vmphase 12405b3
chore: fix links
vmphase d29de3d
Merge branch 'master' into feat/file-types
vmphase 95e4ff1
refactor(file_type): FileType => FileTypeGroup
vmphase 8db8c26
refactor(file_type): make isValid() readable
vmphase 1ec2435
refactor: apply review changes
vmphase 159fef9
Merge branch 'master' into feat/file-types
vmphase File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,49 @@ | ||
| package discord | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| ) | ||
|
|
||
| // MaxFileTypes is the maximum number of file types that can be filtered for at once. | ||
| // https://docs.discord.com/developers/reference#file-type-filtering | ||
| const MaxFileTypes = 10 | ||
|
|
||
| var fileTypeExtensionPattern = regexp.MustCompile(`^\.[\w](?:[\w.-]*[\w])?$`) | ||
|
|
||
| // NewFileType creates a new [FileType] for a custom file extension, e.g. NewFileType(".pdf"). | ||
| // The extension must be dot-prefixed and only contain latin letters, digits, dashes or dots. | ||
| // Use the FileTypeGroupImage, FileTypeGroupVideo or FileTypeGroupAudio constants for the preset file groups. | ||
| func NewFileType(extension string) (FileType, error) { | ||
| if !fileTypeExtensionPattern.MatchString(extension) { | ||
| return "", fmt.Errorf(`file type extension %q must be dot-prefixed (e.g. ".pdf") and only contain latin letters, digits, dashes or dots`, extension) | ||
| } | ||
| return FileType(extension), nil | ||
| } | ||
|
|
||
| // MustNewFileType creates a new [FileType], panicking instead of returning an error if the extension is invalid. | ||
| func MustNewFileType(extension string) FileType { | ||
| fileType, err := NewFileType(extension) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| return fileType | ||
| } | ||
|
|
||
| // FileType represents a type of file to filter for in FileUploadComponents and ApplicationCommandOptionAttachment. | ||
| // It can be a preset group like FileTypeGroupImage, FileTypeGroupVideo or FileTypeGroupAudio, or a custom file extension like ".pdf". | ||
| // File types only match against the file extension, Discord does not validate the actual content of the file. | ||
| // Up to [MaxFileTypes] file types can be specified. | ||
| // https://docs.discord.com/developers/reference#file-type-filtering | ||
| type FileType string | ||
|
|
||
| // Preset FileType groups. | ||
| const ( | ||
| FileTypeGroupImage FileType = "image" | ||
| FileTypeGroupVideo FileType = "video" | ||
| FileTypeGroupAudio FileType = "audio" | ||
| ) | ||
|
|
||
| func (t FileType) String() string { | ||
| return string(t) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.