github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/core/api/endpoint/meta_endpoint.go (about) 1 // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved. 2 // 3 // This software (Documize Community Edition) is licensed under 4 // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html 5 // 6 // You can operate outside the AGPL restrictions by purchasing 7 // Documize Enterprise Edition and obtaining a commercial license 8 // by contacting <sales@documize.com>. 9 // 10 // https://documize.com 11 12 package endpoint 13 14 import ( 15 "bytes" 16 "encoding/json" 17 "fmt" 18 "net/http" 19 "text/template" 20 21 "github.com/documize/community/core" 22 "github.com/documize/community/core/api/entity" 23 "github.com/documize/community/core/api/request" 24 "github.com/documize/community/core/log" 25 "github.com/documize/community/core/utility" 26 ) 27 28 // GetMeta provides org meta data based upon request domain (e.g. acme.documize.com). 29 func GetMeta(w http.ResponseWriter, r *http.Request) { 30 method := "GetMeta" 31 p := request.GetPersister(r) 32 33 data := entity.SiteMeta{} 34 data.URL = request.GetSubdomainFromHost(r) 35 36 org, err := p.GetOrganizationByDomain(data.URL) 37 38 if err != nil { 39 log.Info(fmt.Sprintf("%s URL not found", data.URL)) 40 writeForbiddenError(w) 41 return 42 } 43 44 product := core.Product() 45 46 data.OrgID = org.RefID 47 data.Title = org.Title 48 data.Message = org.Message 49 data.AllowAnonymousAccess = org.AllowAnonymousAccess 50 data.Version = product.Version 51 52 json, err := json.Marshal(data) 53 54 if err != nil { 55 writeJSONMarshalError(w, method, "meta", err) 56 return 57 } 58 59 writeSuccessBytes(w, json) 60 } 61 62 // GetRobots returns robots.txt depending on site configuration. 63 // Did we allow anonymouse access? 64 func GetRobots(w http.ResponseWriter, r *http.Request) { 65 method := "GetRobots" 66 p := request.GetPersister(r) 67 68 domain := request.GetSubdomainFromHost(r) 69 org, err := p.GetOrganizationByDomain(domain) 70 71 // default is to deny 72 robots := 73 `User-agent: * 74 Disallow: / 75 ` 76 77 if err != nil { 78 log.Error(fmt.Sprintf("%s failed to get Organization for domain %s", method, domain), err) 79 } 80 81 // Anonymous access would mean we allow bots to crawl. 82 if org.AllowAnonymousAccess { 83 sitemap := p.Context.GetAppURL("sitemap.xml") 84 robots = fmt.Sprintf( 85 `User-agent: * 86 Disallow: /settings/ 87 Disallow: /settings/* 88 Disallow: /profile/ 89 Disallow: /profile/* 90 Disallow: /auth/login/ 91 Disallow: /auth/login/ 92 Disallow: /auth/logout/ 93 Disallow: /auth/logout/* 94 Disallow: /auth/reset/* 95 Disallow: /auth/reset/* 96 Disallow: /auth/sso/ 97 Disallow: /auth/sso/* 98 Disallow: /share 99 Disallow: /share/* 100 Sitemap: %s`, sitemap) 101 } 102 103 writeSuccessBytes(w, []byte(robots)) 104 } 105 106 // GetSitemap returns URLs that can be indexed. 107 // We only include public folders and documents (e.g. can be seen by everyone). 108 func GetSitemap(w http.ResponseWriter, r *http.Request) { 109 method := "GetSitemap" 110 p := request.GetPersister(r) 111 112 domain := request.GetSubdomainFromHost(r) 113 org, err := p.GetOrganizationByDomain(domain) 114 115 if err != nil { 116 log.Error(fmt.Sprintf("%s failed to get Organization for domain %s", method, domain), err) 117 } 118 119 sitemap := 120 `<?xml version="1.0" encoding="UTF-8"?> 121 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> 122 {{range .}}<url> 123 <loc>{{ .URL }}</loc> 124 <lastmod>{{ .Date }}</lastmod> 125 </url>{{end}} 126 </urlset>` 127 128 var items []sitemapItem 129 130 // Anonymous access means we announce folders/documents shared with 'Everyone'. 131 if org.AllowAnonymousAccess { 132 // Grab shared folders 133 folders, err := p.GetPublicFolders(org.RefID) 134 135 if err != nil { 136 log.Error(fmt.Sprintf("%s failed to get folders for domain %s", method, domain), err) 137 } 138 139 for _, folder := range folders { 140 var item sitemapItem 141 item.URL = p.Context.GetAppURL(fmt.Sprintf("s/%s/%s", folder.RefID, utility.MakeSlug(folder.Name))) 142 item.Date = folder.Revised.Format("2006-01-02T15:04:05.999999-07:00") 143 items = append(items, item) 144 } 145 146 // Grab documents from shared folders 147 documents, err := p.GetPublicDocuments(org.RefID) 148 149 if err != nil { 150 log.Error(fmt.Sprintf("%s failed to get documents for domain %s", method, domain), err) 151 } 152 153 for _, document := range documents { 154 var item sitemapItem 155 item.URL = p.Context.GetAppURL(fmt.Sprintf("s/%s/%s/d/%s/%s", 156 document.FolderID, utility.MakeSlug(document.Folder), document.DocumentID, utility.MakeSlug(document.Document))) 157 item.Date = document.Revised.Format("2006-01-02T15:04:05.999999-07:00") 158 items = append(items, item) 159 } 160 161 } 162 163 buffer := new(bytes.Buffer) 164 t := template.Must(template.New("tmp").Parse(sitemap)) 165 log.IfErr(t.Execute(buffer, &items)) 166 167 writeSuccessBytes(w, buffer.Bytes()) 168 } 169 170 // sitemapItem provides a means to teleport somewhere else for free. 171 // What did you think it did? 172 type sitemapItem struct { 173 URL string 174 Date string 175 }