Пример #1
0
func (e *Editor) serveEditUI(writer http.ResponseWriter, request *http.Request) {
	if !e.store.HasWriteAccessForRequest(request) {
		log.Printf("%s %s: no write permissions", request.Method, request.URL)
		failer.ServeUnauthorized(writer, request)
		return
	}
	if !router.Is(router.ModeCreate, request) && !e.store.HasReadAccessForRequest(request) {
		log.Printf("%s %s: no read permissions", request.Method, request.URL)
		failer.ServeUnauthorized(writer, request)
		return
	}

	mimeType := e.store.MimeTypeForRequest(request)
	if err := e.assertEditableTextFile(request, mimeType); err != nil {
		log.Printf("%s %s: no editable text file: %s", request.Method, request.URL, err)
		failer.ServeUnsupportedMediaType(writer, request)
		return
	}

	var content = e.store.ReadString(request)
	if err := e.store.Err(); err != nil {
		if !store.IsPathNotFoundError(err) {
			log.Printf("%s %s: %s", request.Method, request.URL, err)
			failer.ServeInternalServerError(writer, request)
			return
		} else if router.Is(router.ModeEdit, request) {
			log.Printf("%s %s: file to be edited does not exist: %s", request.Method, request.URL, err)
			failer.ServeNotFound(writer, request)
			return
		}
	} else if router.Is(router.ModeCreate, request) {
		log.Printf("%s %s: file to be created already exists: %s", request.Method, request.URL, err)
		failer.ServeConflict(writer, request)
		return
	}

	err := e.renderer.Render(writer, request.URL, content, mimeType,
		router.Is(router.ModeEdit, request))
	if err != nil {
		log.Printf("%s %s: %s", request.Method, request.URL, err)
		failer.ServeInternalServerError(writer, request)
		return
	}

	log.Printf("%s %s: served from template", request.Method, request.URL)
}
Пример #2
0
func (e *Editor) isServeCreateUI(request *http.Request) bool {
	return request.Method == "GET" && router.Is(router.ModeCreate, request)
}