mirror of
https://github.com/42wim/matterbridge.git
synced 2024-11-15 08:29:25 +01:00
parent
16d5aeac7c
commit
09713d40ba
@ -132,8 +132,17 @@ func (b *Bslack) skipMessageEvent(ev *slack.MessageEvent) bool {
|
|||||||
ev.SubMessage.Edited == nil {
|
ev.SubMessage.Edited == nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
return b.filesCached(ev.Files)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bslack) filesCached(files []slack.File) bool {
|
||||||
|
for i := range files {
|
||||||
|
if !b.fileCached(&files[i]) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// handleMessageEvent handles the message events. Together with any called sub-methods,
|
// handleMessageEvent handles the message events. Together with any called sub-methods,
|
||||||
// this method implements the following event processing pipeline:
|
// this method implements the following event processing pipeline:
|
||||||
@ -248,10 +257,9 @@ func (b *Bslack) handleTypingEvent(ev *slack.UserTypingEvent) (*config.Message,
|
|||||||
|
|
||||||
// handleDownloadFile handles file download
|
// handleDownloadFile handles file download
|
||||||
func (b *Bslack) handleDownloadFile(rmsg *config.Message, file *slack.File) error {
|
func (b *Bslack) handleDownloadFile(rmsg *config.Message, file *slack.File) error {
|
||||||
if b.fileIsAvailable(file) {
|
if b.fileCached(file) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the file is neither too large nor blacklisted.
|
// Check that the file is neither too large nor blacklisted.
|
||||||
if err := helper.HandleDownloadSize(b.Log, rmsg, file.Name, int64(file.Size), b.General); err != nil {
|
if err := helper.HandleDownloadSize(b.Log, rmsg, file.Name, int64(file.Size), b.General); err != nil {
|
||||||
b.Log.WithError(err).Infof("Skipping download of incoming file.")
|
b.Log.WithError(err).Infof("Skipping download of incoming file.")
|
||||||
@ -273,11 +281,18 @@ func (b *Bslack) handleDownloadFile(rmsg *config.Message, file *slack.File) erro
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bslack) fileIsAvailable(file *slack.File) bool {
|
// fileCached implements Matterbridge's caching logic for files
|
||||||
// Only download a file if it is not in the cache or if it has been entered more than a minute ago.
|
// shared via Slack.
|
||||||
if ts, ok := b.cache.Get("file" + file.ID); ok && time.Since(ts.(time.Time)) > time.Minute {
|
//
|
||||||
|
// We consider that a file was cached if its ID was added in the last minute or
|
||||||
|
// it's name was registered in the last 10 seconds. This ensures that an
|
||||||
|
// identically named file but with different content will be uploaded correctly
|
||||||
|
// (the assumption is that such name collisions will not occur within the given
|
||||||
|
// timeframes).
|
||||||
|
func (b *Bslack) fileCached(file *slack.File) bool {
|
||||||
|
if ts, ok := b.cache.Get("file" + file.ID); ok && time.Since(ts.(time.Time)) < time.Minute {
|
||||||
return true
|
return true
|
||||||
} else if ts, ok = b.cache.Get("filename" + file.Name); ok && time.Since(ts.(time.Time)) > 10*time.Second {
|
} else if ts, ok = b.cache.Get("filename" + file.Name); ok && time.Since(ts.(time.Time)) < 10*time.Second {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
@ -384,9 +384,7 @@ func (b *Bslack) uploadFile(msg *config.Message, channelID string) {
|
|||||||
// we can't match on the file ID yet, so we have to match on the filename too.
|
// we can't match on the file ID yet, so we have to match on the filename too.
|
||||||
ts := time.Now()
|
ts := time.Now()
|
||||||
b.Log.Debugf("Adding file %s to cache at %s with timestamp", fi.Name, ts.String())
|
b.Log.Debugf("Adding file %s to cache at %s with timestamp", fi.Name, ts.String())
|
||||||
if !b.cache.Add("filename"+fi.Name, ts) {
|
b.cache.Add("filename"+fi.Name, ts)
|
||||||
b.Log.Warnf("Failed to add file %s to cache at %s with timestamp", fi.Name, ts.String())
|
|
||||||
}
|
|
||||||
res, err := b.sc.UploadFile(slack.FileUploadParameters{
|
res, err := b.sc.UploadFile(slack.FileUploadParameters{
|
||||||
Reader: bytes.NewReader(*fi.Data),
|
Reader: bytes.NewReader(*fi.Data),
|
||||||
Filename: fi.Name,
|
Filename: fi.Name,
|
||||||
@ -399,9 +397,7 @@ func (b *Bslack) uploadFile(msg *config.Message, channelID string) {
|
|||||||
}
|
}
|
||||||
if res.ID != "" {
|
if res.ID != "" {
|
||||||
b.Log.Debugf("Adding file ID %s to cache with timestamp %s", res.ID, ts.String())
|
b.Log.Debugf("Adding file ID %s to cache with timestamp %s", res.ID, ts.String())
|
||||||
if !b.cache.Add("file"+res.ID, ts) {
|
b.cache.Add("file"+res.ID, ts)
|
||||||
b.Log.Warnf("Failed to add file ID %s to cache with timestamp %s", res.ID, ts.String())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user