MCPcopy Index your code
hub / github.com/writefreely/writefreely / handleViewPost

Function handleViewPost

posts.go:313–543  ·  view source on GitHub ↗
(app *App, w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

311}
312
313func handleViewPost(app *App, w http.ResponseWriter, r *http.Request) error {
314 vars := mux.Vars(r)
315 friendlyID := vars["post"]
316
317 // NOTE: until this is done better, be sure to keep this in parity with
318 // isRaw() and viewCollectionPost()
319 isJSON := strings.HasSuffix(friendlyID, ".json")
320 isXML := strings.HasSuffix(friendlyID, ".xml")
321 isCSS := strings.HasSuffix(friendlyID, ".css")
322 isMarkdown := strings.HasSuffix(friendlyID, ".md")
323 isRaw := strings.HasSuffix(friendlyID, ".txt") || isJSON || isXML || isCSS || isMarkdown
324
325 // Display reserved page if that is requested resource
326 if t, ok := pages[r.URL.Path[1:]+".tmpl"]; ok {
327 return handleTemplatedPage(app, w, r, t)
328 } else if r.URL.Path == "/sitemap.xml" && !app.cfg.App.SingleUser {
329 return impart.HTTPError{Status: http.StatusNotFound, Message: "Page not found."}
330 } else if (strings.Contains(r.URL.Path, ".") && !isRaw && !isMarkdown) || r.URL.Path == "/robots.txt" || r.URL.Path == "/manifest.json" {
331 // Serve static file
332 app.shttp.ServeHTTP(w, r)
333 return nil
334 }
335
336 // Display collection if this is a collection
337 c, _ := app.db.GetCollection(friendlyID)
338 if c != nil {
339 return impart.HTTPError{http.StatusMovedPermanently, fmt.Sprintf("/%s/", friendlyID)}
340 }
341
342 // Normalize the URL, redirecting user to consistent post URL
343 if friendlyID != strings.ToLower(friendlyID) {
344 return impart.HTTPError{http.StatusMovedPermanently, fmt.Sprintf("/%s", strings.ToLower(friendlyID))}
345 }
346
347 ext := ""
348 if isRaw {
349 parts := strings.Split(friendlyID, ".")
350 friendlyID = parts[0]
351 if len(parts) > 1 {
352 ext = "." + parts[1]
353 }
354 }
355
356 var ownerID sql.NullInt64
357 var collectionID sql.NullInt64
358 var title string
359 var content string
360 var font string
361 var language []byte
362 var rtl []byte
363 var views int64
364 var post *AnonymousPost
365 var found bool
366 var gone bool
367
368 fixedID := slug.Make(friendlyID)
369 if fixedID != friendlyID {
370 return impart.HTTPError{http.StatusFound, fmt.Sprintf("/%s%s", fixedID, ext)}

Callers

nothing calls this directly

Calls 13

handleTemplatedPageFunction · 0.85
friendlyPostTitleFunction · 0.85
postDescriptionFunction · 0.85
shortPostDescriptionFunction · 0.85
applyMarkdownFunction · 0.85
extractImagesFunction · 0.85
pageForReqFunction · 0.85
getUserSessionFunction · 0.85
IsUserSilencedMethod · 0.80
IsPrivateMethod · 0.80
IsProtectedMethod · 0.80
GetCollectionMethod · 0.65

Tested by

no test coverage detected