diff --git a/http_mutators.go b/http_mutators.go index ca48d07..ea37b9c 100644 --- a/http_mutators.go +++ b/http_mutators.go @@ -30,7 +30,7 @@ func handlerUnattachAsk(w http.ResponseWriter, rq *http.Request) { var ( hyphaName = HyphaNameFromRq(rq, "unattach-ask") hd, isOld = HyphaStorage[hyphaName] - hasAmnt = hd != nil && hd.binaryPath != "" + hasAmnt = hd != nil && hd.BinaryPath != "" ) if !hasAmnt { HttpErr(w, http.StatusBadRequest, hyphaName, "Cannot unattach", "No attachment attached yet, therefore you cannot unattach") @@ -49,7 +49,7 @@ func handlerUnattachConfirm(w http.ResponseWriter, rq *http.Request) { var ( hyphaName = HyphaNameFromRq(rq, "unattach-confirm") hyphaData, isOld = HyphaStorage[hyphaName] - hasAmnt = hyphaData != nil && hyphaData.binaryPath != "" + hasAmnt = hyphaData != nil && hyphaData.BinaryPath != "" u = user.FromRequest(rq) ) if !u.CanProceed("unattach-confirm") { diff --git a/http_readers.go b/http_readers.go index cb1450b..08a5057 100644 --- a/http_readers.go +++ b/http_readers.go @@ -35,8 +35,8 @@ func handlerRevision(w http.ResponseWriter, rq *http.Request) { revHash = shorterUrl[:firstSlashIndex] hyphaName = CanonicalName(shorterUrl[firstSlashIndex+1:]) contents = fmt.Sprintf(`
This hypha had no text at this revision.
`) - textPath = hyphaName + ".myco" - textContents, err = history.FileAtRevision(textPath, revHash) + TextPath = hyphaName + ".myco" + textContents, err = history.FileAtRevision(TextPath, revHash) u = user.FromRequest(rq) ) if err == nil { @@ -61,9 +61,9 @@ func handlerText(w http.ResponseWriter, rq *http.Request) { log.Println(rq.URL) hyphaName := HyphaNameFromRq(rq, "text") if data, ok := HyphaStorage[hyphaName]; ok { - log.Println("Serving", data.textPath) + log.Println("Serving", data.TextPath) w.Header().Set("Content-Type", "text/plain; charset=utf-8") - http.ServeFile(w, rq, data.textPath) + http.ServeFile(w, rq, data.TextPath) } } @@ -72,9 +72,9 @@ func handlerBinary(w http.ResponseWriter, rq *http.Request) { log.Println(rq.URL) hyphaName := HyphaNameFromRq(rq, "binary") if data, ok := HyphaStorage[hyphaName]; ok { - log.Println("Serving", data.binaryPath) - w.Header().Set("Content-Type", mimetype.FromExtension(filepath.Ext(data.binaryPath))) - http.ServeFile(w, rq, data.binaryPath) + log.Println("Serving", data.BinaryPath) + w.Header().Set("Content-Type", mimetype.FromExtension(filepath.Ext(data.BinaryPath))) + http.ServeFile(w, rq, data.BinaryPath) } } @@ -84,14 +84,14 @@ func handlerHypha(w http.ResponseWriter, rq *http.Request) { var ( hyphaName = HyphaNameFromRq(rq, "page", "hypha") data, hyphaExists = HyphaStorage[hyphaName] - hasAmnt = hyphaExists && data.binaryPath != "" + hasAmnt = hyphaExists && data.BinaryPath != "" contents string openGraph string u = user.FromRequest(rq) ) if hyphaExists { - fileContentsT, errT := ioutil.ReadFile(data.textPath) - _, errB := os.Stat(data.binaryPath) + fileContentsT, errT := ioutil.ReadFile(data.TextPath) + _, errB := os.Stat(data.BinaryPath) if errT == nil { md := markup.Doc(hyphaName, string(fileContentsT)) contents = md.AsHTML() diff --git a/hypha.go b/hypha.go index 82fe103..acfa736 100644 --- a/hypha.go +++ b/hypha.go @@ -26,7 +26,7 @@ func init() { markup.HyphaAccess = func(hyphaName string) (rawText, binaryBlock string, err error) { if hyphaData, ok := HyphaStorage[hyphaName]; ok { rawText, err = FetchTextPart(hyphaData) - if hyphaData.binaryPath != "" { + if hyphaData.BinaryPath != "" { binaryBlock = binaryHtmlBlock(hyphaName, hyphaData) } } else { @@ -36,7 +36,7 @@ func init() { } markup.HyphaIterate = IterateHyphaNamesWith markup.HyphaImageForOG = func(hyphaName string) string { - if hd, isOld := GetHyphaData(hyphaName); isOld && hd.binaryPath != "" { + if hd, isOld := GetHyphaData(hyphaName); isOld && hd.BinaryPath != "" { return util.URL + "/binary/" + hyphaName } return util.URL + "/favicon.ico" @@ -53,20 +53,17 @@ func GetHyphaData(hyphaName string) (hyphaData *HyphaData, isOld bool) { } // HyphaData represents a hypha's meta information: binary and text parts rooted paths and content types. -type HyphaData struct { - textPath string - binaryPath string -} +type HyphaData hyphae.Hypha // uploadHelp is a helper function for UploadText and UploadBinary func uploadHelp(hop *history.HistoryOp, hyphaName, ext string, data []byte, u *user.User) *history.HistoryOp { var ( hyphaData, isOld = GetHyphaData(hyphaName) fullPath = filepath.Join(WikiDir, hyphaName+ext) - originalFullPath = &hyphaData.textPath + originalFullPath = &hyphaData.TextPath ) if hop.Type == history.TypeEditBinary { - originalFullPath = &hyphaData.binaryPath + originalFullPath = &hyphaData.BinaryPath } if err := os.MkdirAll(filepath.Dir(fullPath), 0777); err != nil { @@ -121,7 +118,7 @@ func UploadBinary(hyphaName, mime string, file multipart.File, u *user.User) *hi // DeleteHypha deletes hypha and makes a history record about that. func (hd *HyphaData) DeleteHypha(hyphaName string, u *user.User) *history.HistoryOp { hop := history.Operation(history.TypeDeleteHypha). - WithFilesRemoved(hd.textPath, hd.binaryPath). + WithFilesRemoved(hd.TextPath, hd.BinaryPath). WithMsg(fmt.Sprintf("Delete ā%sā", hyphaName)). WithUser(u). Apply() @@ -135,18 +132,18 @@ func (hd *HyphaData) DeleteHypha(hyphaName string, u *user.User) *history.Histor // UnattachHypha unattaches hypha and makes a history record about that. func (hd *HyphaData) UnattachHypha(hyphaName string, u *user.User) *history.HistoryOp { hop := history.Operation(history.TypeUnattachHypha). - WithFilesRemoved(hd.binaryPath). + WithFilesRemoved(hd.BinaryPath). WithMsg(fmt.Sprintf("Unattach ā%sā", hyphaName)). WithUser(u). Apply() if len(hop.Errs) == 0 { hd, ok := HyphaStorage[hyphaName] if ok { - if hd.binaryPath != "" { - hd.binaryPath = "" + if hd.BinaryPath != "" { + hd.BinaryPath = "" } // If nothing is left of the hypha - if hd.textPath == "" { + if hd.TextPath == "" { delete(HyphaStorage, hyphaName) hyphae.DecrementCount() } @@ -170,11 +167,11 @@ func renamingPairs(hyphaNames []string, replaceName func(string) string) (map[st if _, nameUsed := HyphaStorage[replaceName(hn)]; nameUsed { return nil, errors.New("Hypha " + replaceName(hn) + " already exists") } - if hd.textPath != "" { - renameMap[hd.textPath] = replaceName(hd.textPath) + if hd.TextPath != "" { + renameMap[hd.TextPath] = replaceName(hd.TextPath) } - if hd.binaryPath != "" { - renameMap[hd.binaryPath] = replaceName(hd.binaryPath) + if hd.BinaryPath != "" { + renameMap[hd.BinaryPath] = replaceName(hd.BinaryPath) } } } @@ -185,8 +182,8 @@ func renamingPairs(hyphaNames []string, replaceName func(string) string) (map[st func relocateHyphaData(hyphaNames []string, replaceName func(string) string) { for _, hyphaName := range hyphaNames { if hd, ok := HyphaStorage[hyphaName]; ok { - hd.textPath = replaceName(hd.textPath) - hd.binaryPath = replaceName(hd.binaryPath) + hd.TextPath = replaceName(hd.TextPath) + hd.BinaryPath = replaceName(hd.BinaryPath) HyphaStorage[replaceName(hyphaName)] = hd delete(HyphaStorage, hyphaName) } @@ -224,7 +221,7 @@ func RenameHypha(hyphaName, newName string, recursive bool, u *user.User) *histo // binaryHtmlBlock creates an html block for binary part of the hypha. func binaryHtmlBlock(hyphaName string, hd *HyphaData) string { - switch filepath.Ext(hd.binaryPath) { + switch filepath.Ext(hd.BinaryPath) { case ".jpg", ".gif", ".png", ".webp", ".svg", ".ico": return fmt.Sprintf(`