From 620853575be099525cd1530e2e701c4bb7dbf6c0 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Thu, 17 Aug 2023 18:00:46 +0800 Subject: [PATCH 1/2] :art: Support searching some plaintext assets content https://github.com/siyuan-note/siyuan/issues/8987 --- app/src/search/assets.ts | 62 ++++++++++++++--------------------- kernel/model/asset_content.go | 21 +++++++++--- 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/app/src/search/assets.ts b/app/src/search/assets.ts index 523df96f1..ba0fa56e9 100644 --- a/app/src/search/assets.ts +++ b/app/src/search/assets.ts @@ -357,47 +357,35 @@ export const assetMethodMenu = (target: HTMLElement, cb: () => void) => { window.siyuan.menus.menu.popup({x: rect.right, y: rect.bottom}, true); }; +let filterTypes: string[] = [ + ".txt", ".md", ".markdown", ".docx", ".xlsx", ".pptx", ".json", ".log", ".sql", ".html", ".xml", ".java", ".h", ".c", + ".cpp", ".go", ".rs", ".swift", ".kt", ".py", ".php", ".js", ".css", ".ts", ".sh", ".bat", ".cmd", ".ini", ".yaml", + ".rst", ".adoc", ".textile", ".opml", ".org", ".wiki", +] + +const filterTypesHTML = (types: string[]) => { + filterTypes = filterTypes.sort((a: string, b: string) => { + return a.localeCompare(b); + }); + + let html = ""; + types.forEach((type: string) => { + html += ``; + }); + return html; +} + export const assetFilterMenu = (assetsElement: Element) => { const localData = window.siyuan.storage[Constants.LOCAL_SEARCHASSET].types; const filterDialog = new Dialog({ title: window.siyuan.languages.type, - content: `
- - - - - -
+ content: `
` + filterTypesHTML(filterTypes) + `
diff --git a/kernel/model/asset_content.go b/kernel/model/asset_content.go index a31163f27..ff45540c7 100644 --- a/kernel/model/asset_content.go +++ b/kernel/model/asset_content.go @@ -289,7 +289,7 @@ func IndexAssetContent(absPath string) { assetsDir := util.GetDataAssetsAbsPath() - ext := strings.ToLower(filepath.Ext(absPath)) + ext := filepath.Ext(absPath) parser := assetContentSearcher.GetParser(ext) if nil == parser { return @@ -360,7 +360,7 @@ func (searcher *AssetsSearcher) GetParser(ext string) AssetParser { searcher.lock.Lock() defer searcher.lock.Unlock() - return searcher.parsers[ext] + return searcher.parsers[strings.ToLower(ext)] } func (searcher *AssetsSearcher) FullIndex() { @@ -382,7 +382,7 @@ func (searcher *AssetsSearcher) FullIndex() { return nil } - ext := strings.ToLower(filepath.Ext(absPath)) + ext := filepath.Ext(absPath) parser := searcher.GetParser(ext) if nil == parser { return nil @@ -433,9 +433,11 @@ func NewAssetsSearcher() *AssetsSearcher { ".c": txtAssetParser, ".cpp": txtAssetParser, ".go": txtAssetParser, + ".rs": txtAssetParser, ".swift": txtAssetParser, ".kt": txtAssetParser, ".py": txtAssetParser, + ".php": txtAssetParser, ".js": txtAssetParser, ".css": txtAssetParser, ".ts": txtAssetParser, @@ -460,6 +462,10 @@ func NewAssetsSearcher() *AssetsSearcher { } } +const ( + TxtAssetContentMaxSize = 1024 * 1024 * 4 +) + type AssetParseResult struct { Path string Size int64 @@ -475,7 +481,14 @@ type TxtAssetParser struct { } func (parser *TxtAssetParser) Parse(absPath string) (ret *AssetParseResult) { - if !strings.HasSuffix(strings.ToLower(absPath), ".txt") { + info, err := os.Stat(absPath) + if nil != err { + logging.LogErrorf("stat file [%s] failed: %s", absPath, err) + return + } + + if TxtAssetContentMaxSize < info.Size() { + logging.LogWarnf("file [%s] is too large [%s]", absPath, humanize.Bytes(uint64(info.Size()))) return } From 1238e03157adad6172f9c61bf6d83b9974a555ed Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Thu, 17 Aug 2023 18:04:35 +0800 Subject: [PATCH 2/2] :art: Support searching PDF asset content https://github.com/siyuan-note/siyuan/pull/8985 --- app/src/search/assets.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/search/assets.ts b/app/src/search/assets.ts index ba0fa56e9..e07703463 100644 --- a/app/src/search/assets.ts +++ b/app/src/search/assets.ts @@ -358,7 +358,7 @@ export const assetMethodMenu = (target: HTMLElement, cb: () => void) => { }; let filterTypes: string[] = [ - ".txt", ".md", ".markdown", ".docx", ".xlsx", ".pptx", ".json", ".log", ".sql", ".html", ".xml", ".java", ".h", ".c", + ".txt", ".md", ".markdown", ".docx", ".xlsx", ".pptx", ".pdf", ".json", ".log", ".sql", ".html", ".xml", ".java", ".h", ".c", ".cpp", ".go", ".rs", ".swift", ".kt", ".py", ".php", ".js", ".css", ".ts", ".sh", ".bat", ".cmd", ".ini", ".yaml", ".rst", ".adoc", ".textile", ".opml", ".org", ".wiki", ]