MCPcopy Create free account
hub / github.com/daodst/chat / fetchRemoteFileAndStoreMetadata

Method fetchRemoteFileAndStoreMetadata

mediaapi/routing/download.go:616–675  ·  view source on GitHub ↗

fetchRemoteFileAndStoreMetadata fetches the file from the remote server and stores its metadata in the database

(
	ctx context.Context,
	client *gomatrixserverlib.Client,
	absBasePath config.Path,
	maxFileSizeBytes config.FileSizeBytes,
	db storage.Database,
	thumbnailSizes []config.ThumbnailSize,
	activeThumbnailGeneration *types.ActiveThumbnailGeneration,
	maxThumbnailGenerators int,
)

Source from the content-addressed store, hash-verified

614
615// fetchRemoteFileAndStoreMetadata fetches the file from the remote server and stores its metadata in the database
616func (r *downloadRequest) fetchRemoteFileAndStoreMetadata(
617 ctx context.Context,
618 client *gomatrixserverlib.Client,
619 absBasePath config.Path,
620 maxFileSizeBytes config.FileSizeBytes,
621 db storage.Database,
622 thumbnailSizes []config.ThumbnailSize,
623 activeThumbnailGeneration *types.ActiveThumbnailGeneration,
624 maxThumbnailGenerators int,
625) error {
626 finalPath, duplicate, err := r.fetchRemoteFile(
627 ctx, client, absBasePath, maxFileSizeBytes,
628 )
629 if err != nil {
630 return err
631 }
632
633 r.Logger.WithFields(log.Fields{
634 "Base64Hash": r.MediaMetadata.Base64Hash,
635 "UploadName": r.MediaMetadata.UploadName,
636 "FileSizeBytes": r.MediaMetadata.FileSizeBytes,
637 "ContentType": r.MediaMetadata.ContentType,
638 }).Debug("Storing file metadata to media repository database")
639
640 // FIXME: timeout db request
641 if err := db.StoreMediaMetadata(ctx, r.MediaMetadata); err != nil {
642 // If the file is a duplicate (has the same hash as an existing file) then
643 // there is valid metadata in the database for that file. As such we only
644 // remove the file if it is not a duplicate.
645 if !duplicate {
646 finalDir := filepath.Dir(string(finalPath))
647 fileutils.RemoveDir(types.Path(finalDir), r.Logger)
648 }
649 // NOTE: It should really not be possible to fail the uniqueness test here so
650 // there is no need to handle that separately
651 return errors.New("failed to store file metadata in DB")
652 }
653
654 go func() {
655 busy, err := thumbnailer.GenerateThumbnails(
656 context.Background(), finalPath, thumbnailSizes, r.MediaMetadata,
657 activeThumbnailGeneration, maxThumbnailGenerators, db, r.Logger,
658 )
659 if err != nil {
660 r.Logger.WithError(err).Warn("Error generating thumbnails")
661 }
662 if busy {
663 r.Logger.Warn("Maximum number of active thumbnail generators reached. Skipping pre-generation.")
664 }
665 }()
666
667 r.Logger.WithFields(log.Fields{
668 "UploadName": r.MediaMetadata.UploadName,
669 "Base64Hash": r.MediaMetadata.Base64Hash,
670 "FileSizeBytes": r.MediaMetadata.FileSizeBytes,
671 "ContentType": r.MediaMetadata.ContentType,
672 }).Debug("Remote file cached")
673

Callers 1

getRemoteFileMethod · 0.95

Calls 2

fetchRemoteFileMethod · 0.95
StoreMediaMetadataMethod · 0.65

Tested by

no test coverage detected