123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- package modules
- import (
- "net/http"
- "os"
- "golang-fave/assets"
- "golang-fave/engine/fetdata"
- "golang-fave/engine/wrapper"
- "golang-fave/utils"
- )
- func (this *Modules) RegisterModule_Api() *Module {
- return this.newModule(MInfo{
- WantDB: true,
- Mount: "api",
- Name: "Api",
- Order: 803,
- System: true,
- Icon: assets.SysSvgIconPage,
- Sub: &[]MISub{},
- }, func(wrap *wrapper.Wrapper) {
- if len(wrap.UrlArgs) == 5 && wrap.UrlArgs[0] == "api" && wrap.UrlArgs[1] == "product-image" && (wrap.UrlArgs[2] == "thumb-0" || wrap.UrlArgs[2] == "thumb-1" || wrap.UrlArgs[2] == "thumb-2" || wrap.UrlArgs[2] == "thumb-3" || wrap.UrlArgs[2] == "thumb-full") {
- thumb_type := wrap.UrlArgs[2]
- product_id := wrap.UrlArgs[3]
- file_name := wrap.UrlArgs[4]
- original_file := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + product_id + string(os.PathSeparator) + file_name
- if !utils.IsFileExists(original_file) {
- // User error 404 page
- wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
- return
- }
- width := (*wrap.Config).Shop.Thumbnails.Thumbnail0[0]
- height := (*wrap.Config).Shop.Thumbnails.Thumbnail0[1]
- resize := false
- if thumb_type == "thumb-1" {
- width = (*wrap.Config).Shop.Thumbnails.Thumbnail1[0]
- height = (*wrap.Config).Shop.Thumbnails.Thumbnail1[1]
- if (*wrap.Config).Shop.Thumbnails.Thumbnail1[2] == 1 {
- resize = true
- }
- } else if thumb_type == "thumb-2" {
- width = (*wrap.Config).Shop.Thumbnails.Thumbnail2[0]
- height = (*wrap.Config).Shop.Thumbnails.Thumbnail2[1]
- if (*wrap.Config).Shop.Thumbnails.Thumbnail2[2] == 1 {
- resize = true
- }
- } else if thumb_type == "thumb-3" {
- width = (*wrap.Config).Shop.Thumbnails.Thumbnail3[0]
- height = (*wrap.Config).Shop.Thumbnails.Thumbnail3[1]
- if (*wrap.Config).Shop.Thumbnails.Thumbnail3[2] == 1 {
- resize = true
- }
- } else if thumb_type == "thumb-full" {
- width = (*wrap.Config).Shop.Thumbnails.ThumbnailFull[0]
- height = (*wrap.Config).Shop.Thumbnails.ThumbnailFull[1]
- if (*wrap.Config).Shop.Thumbnails.ThumbnailFull[2] == 1 {
- resize = true
- }
- }
- target_file := wrap.DHtdocs + string(os.PathSeparator) + "products" + string(os.PathSeparator) + "images" + string(os.PathSeparator) + product_id + string(os.PathSeparator) + thumb_type + "-" + file_name
- if !utils.IsFileExists(target_file) {
- data, ok, ext, err := this.api_GenerateImage(wrap, width, height, resize, original_file)
- if err != nil {
- // System error 500
- utils.SystemErrorPageEngine(wrap.W, err)
- return
- }
- if !ok {
- // User error 404 page
- wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
- return
- }
- // Save file
- if file, err := os.Create(target_file); err == nil {
- file.Write(data)
- }
- wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
- wrap.W.Header().Set("Content-Type", ext)
- wrap.W.Write(data)
- } else {
- http.ServeFile(wrap.W, wrap.R, target_file)
- }
- } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "api" && wrap.UrlArgs[1] == "products" {
- if (*wrap.Config).API.XML.Enabled == 1 {
- // Fix url
- if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
- http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
- return
- }
- // XML
- wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
- wrap.W.Header().Set("Content-Type", "text/xml; charset=utf-8")
- wrap.W.WriteHeader(http.StatusOK)
- wrap.W.Write([]byte(this.api_GenerateXml(wrap)))
- } else {
- wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
- wrap.W.WriteHeader(http.StatusNotFound)
- wrap.W.Write([]byte("Disabled!"))
- }
- } else if len(wrap.UrlArgs) == 1 {
- // Fix url
- if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
- http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
- return
- }
- // Some info
- wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
- wrap.W.WriteHeader(http.StatusOK)
- wrap.W.Write([]byte("Fave engine API mount point!"))
- } else {
- // User error 404 page
- wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
- return
- }
- }, func(wrap *wrapper.Wrapper) (string, string, string) {
- // No any page for back-end
- return "", "", ""
- })
- }
|