Display count of file uses

This commit is contained in:
Jan-Lukas Else 2021-06-29 18:11:42 +02:00
parent 804ff1c221
commit 4c5e77dcab
8 changed files with 85 additions and 19 deletions

View File

@ -70,7 +70,7 @@ func (a *goBlog) openDatabase(file string, logging bool) (*database, error) {
return nil
},
}
if a.cfg.Db.Debug {
if c := a.cfg.Db; c != nil && c.Debug {
dr = sqlhooks.Wrap(dr, &dbHooks{})
}
sql.Register("goblog_db_"+dbDriverName, dr)

View File

@ -10,7 +10,9 @@ func (a *goBlog) setInMemoryDatabase() {
func Test_database(t *testing.T) {
t.Run("Basic Database Test", func(t *testing.T) {
app := &goBlog{}
app := &goBlog{
cfg: &config{},
}
db, err := app.openDatabase(":memory:", false)
if err != nil {

View File

@ -428,3 +428,16 @@ func (d *database) allPublishedDates(blog string) (dates []publishedDate, err er
}
return
}
func (db *database) usesOfMediaFile(name string) (count int, err error) {
query := "select count(distinct path) from (select path from posts where instr(content, @name) > 0 union all select path from post_parameters where instr(value, @name) > 0)"
row, err := db.queryRow(query, sql.Named("name", name))
if err != nil {
return 0, err
}
err = row.Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}

View File

@ -155,7 +155,9 @@ func Test_postsDb(t *testing.T) {
func Test_ftsWithoutTitle(t *testing.T) {
// Added because there was a bug where there were no search results without title
app := &goBlog{}
app := &goBlog{
cfg: &config{},
}
app.setInMemoryDatabase()
err := app.db.savePost(&post{
@ -175,3 +177,49 @@ func Test_ftsWithoutTitle(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, ps, 1)
}
func Test_usesOfMediaFile(t *testing.T) {
app := &goBlog{
cfg: &config{},
}
app.setInMemoryDatabase()
err := app.db.savePost(&post{
Path: "/test/abc",
Content: "ABC test.jpg DEF",
Published: toLocalSafe(time.Now().String()),
Blog: "en",
Section: "test",
Status: statusDraft,
}, &postCreationOptions{new: true})
require.NoError(t, err)
err = app.db.savePost(&post{
Path: "/test/def",
Content: "ABCDEF",
Published: toLocalSafe(time.Now().String()),
Blog: "en",
Section: "test",
Status: statusDraft,
Parameters: map[string][]string{
"test": {
"https://example.com/test.jpg",
},
},
}, &postCreationOptions{new: true})
require.NoError(t, err)
err = app.db.savePost(&post{
Path: "/test/hij",
Content: "ABCDEF",
Published: toLocalSafe(time.Now().String()),
Blog: "en",
Section: "test",
Status: statusDraft,
}, &postCreationOptions{new: true})
require.NoError(t, err)
count, err := app.db.usesOfMediaFile("test.jpg")
require.NoError(t, err)
assert.Equal(t, 2, count)
}

View File

@ -55,21 +55,22 @@ func (a *goBlog) initRendering() error {
"translations": a.postTranslations,
"shorturl": a.shortPostURL,
// Others
"dateformat": dateFormat,
"isodate": isoDateFormat,
"unixtodate": unixToLocalDateString,
"now": localNowString,
"asset": a.assetFileName,
"string": a.ts.GetTemplateStringVariantFunc(),
"include": a.includeRenderedTemplate,
"urlize": urlize,
"sort": sortedStrings,
"absolute": a.getFullAddress,
"mentions": a.db.getWebmentionsByAddress,
"geotitle": a.geoTitle,
"geolink": geoOSMLink,
"opensearch": openSearchUrl,
"mbytes": mBytesString,
"dateformat": dateFormat,
"isodate": isoDateFormat,
"unixtodate": unixToLocalDateString,
"now": localNowString,
"asset": a.assetFileName,
"string": a.ts.GetTemplateStringVariantFunc(),
"include": a.includeRenderedTemplate,
"urlize": urlize,
"sort": sortedStrings,
"absolute": a.getFullAddress,
"mentions": a.db.getWebmentionsByAddress,
"geotitle": a.geoTitle,
"geolink": geoOSMLink,
"opensearch": openSearchUrl,
"mbytes": mBytesString,
"mediafileuses": a.db.usesOfMediaFile,
}
baseTemplate, err := template.New("base").Funcs(templateFunctions).ParseFiles(path.Join(templatesDir, templateBase+templatesExt))
if err != nil {

View File

@ -10,7 +10,7 @@
<form class="fw-form p" method="post">
<select name="filename">
{{ range $i, $file := .Data.Files }}
<option value="{{ $file.Name }}">{{ $file.Name }} ({{ isodate $file.Time.String }}, {{ mbytes $file.Size }})</option>
<option value="{{ $file.Name }}">{{ $file.Name }} ({{ isodate $file.Time.String }}, {{ mbytes $file.Size }}, ~{{ mediafileuses $file.Name }} {{ string $blog.Lang "fileuses" }})</option>
{{ end }}
</select>
<input type="submit" formaction="{{ .Blog.RelativePath "/editor/files/view" }}" value="{{ string .Blog.Lang "view" }}">

View File

@ -11,6 +11,7 @@ docomment: "Kommentieren"
download: "Herunterladen"
drafts: "Entwürfe"
editor: "Editor"
fileuses: "Datei-Verwendungen"
interactions: "Interaktionen & Kommentare"
interactionslabel: "Hast du eine Antwort hierzu veröffentlicht? Füge hier die URL ein."
likeof: "Gefällt mir von"

View File

@ -17,6 +17,7 @@ download: "Download"
drafts: "Drafts"
editor: "Editor"
feed: "Feed"
fileuses: "file uses"
indieauth: "IndieAuth"
interactions: "Interactions & Comments"
interactionslabel: "Have you published a response to this? Paste the URL here."