mirror of https://github.com/jlelse/GoBlog
Display count of file uses
This commit is contained in:
parent
804ff1c221
commit
4c5e77dcab
|
@ -70,7 +70,7 @@ func (a *goBlog) openDatabase(file string, logging bool) (*database, error) {
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if a.cfg.Db.Debug {
|
if c := a.cfg.Db; c != nil && c.Debug {
|
||||||
dr = sqlhooks.Wrap(dr, &dbHooks{})
|
dr = sqlhooks.Wrap(dr, &dbHooks{})
|
||||||
}
|
}
|
||||||
sql.Register("goblog_db_"+dbDriverName, dr)
|
sql.Register("goblog_db_"+dbDriverName, dr)
|
||||||
|
|
|
@ -10,7 +10,9 @@ func (a *goBlog) setInMemoryDatabase() {
|
||||||
|
|
||||||
func Test_database(t *testing.T) {
|
func Test_database(t *testing.T) {
|
||||||
t.Run("Basic Database Test", func(t *testing.T) {
|
t.Run("Basic Database Test", func(t *testing.T) {
|
||||||
app := &goBlog{}
|
app := &goBlog{
|
||||||
|
cfg: &config{},
|
||||||
|
}
|
||||||
|
|
||||||
db, err := app.openDatabase(":memory:", false)
|
db, err := app.openDatabase(":memory:", false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
13
postsDb.go
13
postsDb.go
|
@ -428,3 +428,16 @@ func (d *database) allPublishedDates(blog string) (dates []publishedDate, err er
|
||||||
}
|
}
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -155,7 +155,9 @@ func Test_postsDb(t *testing.T) {
|
||||||
func Test_ftsWithoutTitle(t *testing.T) {
|
func Test_ftsWithoutTitle(t *testing.T) {
|
||||||
// Added because there was a bug where there were no search results without title
|
// Added because there was a bug where there were no search results without title
|
||||||
|
|
||||||
app := &goBlog{}
|
app := &goBlog{
|
||||||
|
cfg: &config{},
|
||||||
|
}
|
||||||
app.setInMemoryDatabase()
|
app.setInMemoryDatabase()
|
||||||
|
|
||||||
err := app.db.savePost(&post{
|
err := app.db.savePost(&post{
|
||||||
|
@ -175,3 +177,49 @@ func Test_ftsWithoutTitle(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Len(t, ps, 1)
|
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)
|
||||||
|
}
|
||||||
|
|
31
render.go
31
render.go
|
@ -55,21 +55,22 @@ func (a *goBlog) initRendering() error {
|
||||||
"translations": a.postTranslations,
|
"translations": a.postTranslations,
|
||||||
"shorturl": a.shortPostURL,
|
"shorturl": a.shortPostURL,
|
||||||
// Others
|
// Others
|
||||||
"dateformat": dateFormat,
|
"dateformat": dateFormat,
|
||||||
"isodate": isoDateFormat,
|
"isodate": isoDateFormat,
|
||||||
"unixtodate": unixToLocalDateString,
|
"unixtodate": unixToLocalDateString,
|
||||||
"now": localNowString,
|
"now": localNowString,
|
||||||
"asset": a.assetFileName,
|
"asset": a.assetFileName,
|
||||||
"string": a.ts.GetTemplateStringVariantFunc(),
|
"string": a.ts.GetTemplateStringVariantFunc(),
|
||||||
"include": a.includeRenderedTemplate,
|
"include": a.includeRenderedTemplate,
|
||||||
"urlize": urlize,
|
"urlize": urlize,
|
||||||
"sort": sortedStrings,
|
"sort": sortedStrings,
|
||||||
"absolute": a.getFullAddress,
|
"absolute": a.getFullAddress,
|
||||||
"mentions": a.db.getWebmentionsByAddress,
|
"mentions": a.db.getWebmentionsByAddress,
|
||||||
"geotitle": a.geoTitle,
|
"geotitle": a.geoTitle,
|
||||||
"geolink": geoOSMLink,
|
"geolink": geoOSMLink,
|
||||||
"opensearch": openSearchUrl,
|
"opensearch": openSearchUrl,
|
||||||
"mbytes": mBytesString,
|
"mbytes": mBytesString,
|
||||||
|
"mediafileuses": a.db.usesOfMediaFile,
|
||||||
}
|
}
|
||||||
baseTemplate, err := template.New("base").Funcs(templateFunctions).ParseFiles(path.Join(templatesDir, templateBase+templatesExt))
|
baseTemplate, err := template.New("base").Funcs(templateFunctions).ParseFiles(path.Join(templatesDir, templateBase+templatesExt))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
<form class="fw-form p" method="post">
|
<form class="fw-form p" method="post">
|
||||||
<select name="filename">
|
<select name="filename">
|
||||||
{{ range $i, $file := .Data.Files }}
|
{{ 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 }}
|
{{ end }}
|
||||||
</select>
|
</select>
|
||||||
<input type="submit" formaction="{{ .Blog.RelativePath "/editor/files/view" }}" value="{{ string .Blog.Lang "view" }}">
|
<input type="submit" formaction="{{ .Blog.RelativePath "/editor/files/view" }}" value="{{ string .Blog.Lang "view" }}">
|
||||||
|
|
|
@ -11,6 +11,7 @@ docomment: "Kommentieren"
|
||||||
download: "Herunterladen"
|
download: "Herunterladen"
|
||||||
drafts: "Entwürfe"
|
drafts: "Entwürfe"
|
||||||
editor: "Editor"
|
editor: "Editor"
|
||||||
|
fileuses: "Datei-Verwendungen"
|
||||||
interactions: "Interaktionen & Kommentare"
|
interactions: "Interaktionen & Kommentare"
|
||||||
interactionslabel: "Hast du eine Antwort hierzu veröffentlicht? Füge hier die URL ein."
|
interactionslabel: "Hast du eine Antwort hierzu veröffentlicht? Füge hier die URL ein."
|
||||||
likeof: "Gefällt mir von"
|
likeof: "Gefällt mir von"
|
||||||
|
|
|
@ -17,6 +17,7 @@ download: "Download"
|
||||||
drafts: "Drafts"
|
drafts: "Drafts"
|
||||||
editor: "Editor"
|
editor: "Editor"
|
||||||
feed: "Feed"
|
feed: "Feed"
|
||||||
|
fileuses: "file uses"
|
||||||
indieauth: "IndieAuth"
|
indieauth: "IndieAuth"
|
||||||
interactions: "Interactions & Comments"
|
interactions: "Interactions & Comments"
|
||||||
interactionslabel: "Have you published a response to this? Paste the URL here."
|
interactionslabel: "Have you published a response to this? Paste the URL here."
|
||||||
|
|
Loading…
Reference in New Issue