-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adam Collins
committed
Oct 4, 2023
1 parent
aa3719e
commit 730d0b2
Showing
10 changed files
with
196 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
grails-app/controllers/au/org/ala/collectory/SitemapController.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package au.org.ala.collectory | ||
|
||
class SitemapController { | ||
|
||
def index(Integer idx) { | ||
if (!grailsApplication.config.sitemap.enabled) { | ||
response.status = 404 | ||
return | ||
} | ||
|
||
File index = new File(grailsApplication.config.sitemap.dir + '/sitemap.xml') | ||
if (!index.exists()) { | ||
response.status = 404 | ||
return | ||
} | ||
|
||
if (idx == null) { | ||
// return sitemap index | ||
response.outputStream << index.newInputStream() | ||
} else { | ||
// return sitemap urls | ||
File part = new File(grailsApplication.config.sitemap.dir + '/sitemap' + idx + ".xml") | ||
if (!part.exists()) { | ||
response.status = 404 | ||
return | ||
} | ||
response.outputStream << part.newInputStream() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
grails-app/services/au/org/ala/collectory/SitemapService.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright (C) 2022 Atlas of Living Australia | ||
* All Rights Reserved. | ||
* | ||
* The contents of this file are subject to the Mozilla Public | ||
* License Version 1.1 (the "License"); you may not use this file | ||
* except in compliance with the License. You may obtain a copy of | ||
* the License at http://www.mozilla.org/MPL/ | ||
* | ||
* Software distributed under the License is distributed on an "AS | ||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* rights and limitations under the License. | ||
*/ | ||
package au.org.ala.collectory | ||
|
||
import org.springframework.scheduling.annotation.Scheduled | ||
|
||
import java.text.SimpleDateFormat | ||
|
||
class SitemapService { | ||
|
||
def grailsApplication | ||
|
||
|
||
String URLSET_HEADER = "<?xml version='1.0' encoding='UTF-8'?><urlset 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\" xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">" | ||
String URLSET_FOOTER = "</urlset>" | ||
|
||
int MAX_URLS = 50000 // maximum number of URLs in a sitemap file | ||
int MAX_SIZE = 9*1024*1024 // use 9MB to keep the actual file size below 10MB (a gateway limit) | ||
|
||
File currentFile | ||
int fileCount = 0 | ||
int countUrls = 0 | ||
|
||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd") | ||
|
||
FileWriter fw | ||
|
||
// run daily, initial delay 1hr | ||
@Scheduled(fixedDelay = 86400000L, initialDelay = 3600000L) | ||
def build() throws Exception { | ||
initWriter() | ||
buildSitemap() | ||
closeWriter() | ||
|
||
buildSitemapIndex() | ||
} | ||
|
||
def buildSitemapIndex() { | ||
|
||
// write parent sitemap file | ||
fw = new FileWriter(grailsApplication.config.sitemap.dir + "/sitemap.xml") | ||
fw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">") | ||
|
||
for (int i=0;i<fileCount;i++) { | ||
|
||
// move the tmp file | ||
File newFile = new File(grailsApplication.config.sitemap.dir + "/sitemap" + i + ".xml") | ||
if (newFile.exists()) { | ||
newFile.delete() | ||
} | ||
new File(grailsApplication.config.sitemap.dir + "/sitemap" + i + ".xml.tmp").renameTo(newFile) | ||
|
||
// add an entry for this new file | ||
fw.write("<sitemap><url>" + grailsApplication.config.grails.serverURL + "/sitemap" + i + ".xml" + "</url>") | ||
fw.write("<lastmod>" + simpleDateFormat.format(new Date()) + "</lastmod></sitemap>") | ||
} | ||
|
||
fw.write("</sitemapindex>") | ||
fw.flush() | ||
fw.close() | ||
} | ||
|
||
def initWriter() { | ||
currentFile = new File(grailsApplication.config.sitemap.dir + "/sitemap" + fileCount + ".xml.tmp") | ||
|
||
fw = new FileWriter(currentFile) | ||
|
||
fw.write(URLSET_HEADER) | ||
|
||
countUrls = 0 | ||
fileCount++ | ||
} | ||
|
||
def closeWriter() { | ||
fw.write(URLSET_FOOTER) | ||
fw.flush() | ||
fw.close() | ||
} | ||
|
||
def writeUrl(Date lastUpdated, String changefreq, String encodedUrl) { | ||
if (countUrls >= MAX_URLS || currentFile.size() >= MAX_SIZE) { | ||
closeWriter() | ||
initWriter() | ||
} | ||
|
||
fw.write("<url>") | ||
fw.write("<loc>" + encodedUrl + "</loc>") | ||
fw.write("<lastmod>" + simpleDateFormat.format(lastUpdated) + "</lastmod>") | ||
fw.write("<changefreq>" + changefreq + "</changefreq>") | ||
fw.write("</url>") | ||
|
||
fw.flush() | ||
|
||
countUrls++ | ||
} | ||
|
||
def buildSitemap() throws Exception { | ||
|
||
Collection.findAll().each {Collection it -> | ||
writeUrl(it.lastUpdated, "weekly", grailsApplication.config.grails.serverURL + "/public/show/" + it.id) | ||
} | ||
|
||
Institution.findAll().each {Institution it -> | ||
writeUrl(it.lastUpdated, "weekly", grailsApplication.config.grails.serverURL + "/public/show/" + it.id) | ||
} | ||
|
||
DataProvider.findAll().each {DataProvider it -> | ||
writeUrl(it.lastUpdated, "weekly", grailsApplication.config.grails.serverURL + "/public/show/" + it.id) | ||
} | ||
|
||
DataResource.findAllByIsPrivate(false).each {DataResource it -> | ||
writeUrl(it.lastUpdated, "weekly", grailsApplication.config.grails.serverURL + "/public/show/" + it.id) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters