github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/services/docextractor/combine.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package docextractor
     5  
     6  import (
     7  	"io"
     8  
     9  	"github.com/masterhung0112/hk_server/v5/shared/mlog"
    10  )
    11  
    12  type combineExtractor struct {
    13  	SubExtractors []Extractor
    14  }
    15  
    16  func (ce *combineExtractor) Add(extractor Extractor) {
    17  	ce.SubExtractors = append(ce.SubExtractors, extractor)
    18  }
    19  
    20  func (ce *combineExtractor) Match(filename string) bool {
    21  	for _, extractor := range ce.SubExtractors {
    22  		if extractor.Match(filename) {
    23  			return true
    24  		}
    25  	}
    26  	return false
    27  }
    28  
    29  func (ce *combineExtractor) Extract(filename string, r io.ReadSeeker) (string, error) {
    30  	for _, extractor := range ce.SubExtractors {
    31  		if extractor.Match(filename) {
    32  			r.Seek(0, io.SeekStart)
    33  			text, err := extractor.Extract(filename, r)
    34  			if err != nil {
    35  				mlog.Warn("unable to extract file content", mlog.Err(err))
    36  				continue
    37  			}
    38  			return text, nil
    39  		}
    40  	}
    41  	return "", nil
    42  }