From bea7c28af2f714474799627b6c0b53107fe90b9a Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Wed, 21 Jan 2026 18:26:48 +0000 Subject: [PATCH] Add image lookup on status page --- htmlpage/static/js/map-single-point.js | 118 +++++++++++++++++++++++ public-report/image.go | 18 ++++ public-report/routes.go | 1 + public-report/status.go | 22 ++++- public-report/template/status-by-id.html | 27 +++++- userfile/userfile.go | 38 ++++++++ 6 files changed, 216 insertions(+), 8 deletions(-) create mode 100644 htmlpage/static/js/map-single-point.js create mode 100644 public-report/image.go diff --git a/htmlpage/static/js/map-single-point.js b/htmlpage/static/js/map-single-point.js new file mode 100644 index 00000000..e191c68a --- /dev/null +++ b/htmlpage/static/js/map-single-point.js @@ -0,0 +1,118 @@ +var map = null; +// A map that just shows a single point location, and can't be moved +class MapSinglePoint extends HTMLElement { + constructor() { + super(); + + // Create a shadow DOM + this.attachShadow({mode: "open" }); + + // Initial render + this.render(); + + // markers shown on the map. Should be none or 1, generally. + this._markers = null; + } + + // Lifecycle: when element is added to the DOM + connectedCallback() { + // Initialize the map when the element is added to the DOM + setTimeout(() => this._initializeMap(), 0); + } + + disconnectedCallback() { + if (this._map) { + this._map.remove(); + } + } + + _initializeMap() { + console.log("Setting up the map..."); + const apiKey = this.getAttribute("api-key"); + const lat = Number(this.getAttribute('latitude') || 36.2); + const lng = Number(this.getAttribute('longitude') || -119.2); + const zoom = Number(this.getAttribute('zoom') || 15); + + mapboxgl.accessToken = apiKey; + const mapElement = this.shadowRoot.querySelector("#map"); + map = new mapboxgl.Map({ + container: mapElement, + center: { + lat: lat, + lng: lng, + }, + style: 'mapbox://styles/mapbox/streets-v12', // style URL + zoom: zoom, + }); + map.on("load", function() { + this.dispatchEvent(new CustomEvent('load'), { + bubbles: true, + composed: true, // Allows event to cross shadow DOM boundary + detail: { + map: this + } + }); + }); + } + + // Initial render of component + render() { + this.shadowRoot.innerHTML = ` + + +
+
+
+ `; + } + + jumpTo(args) { + this._map.jumpTo(args); + } + + setMarker(coords) { + console.log("Setting map marker", coords); + this._map.jumpTo({ + center: coords, + zoom: 14, + }); + this._markers.forEach((marker) => marker.remove()); + + const marker = new mapboxgl.Marker({ + color: "#FF0000", + draggable: true + }).setLngLat(coords).addTo(map); + marker.on('dragend', function(e) { + const markerDraggedEvent = new CustomEvent("markerdragend", { + detail: { + marker: marker + } + }); + mapContainer.dispatchEvent(markerDraggedEvent); + }); + this._markers = [marker]; + } +} + +customElements.define('map-single-point', MapSinglePoint); diff --git a/public-report/image.go b/public-report/image.go new file mode 100644 index 00000000..b14acd49 --- /dev/null +++ b/public-report/image.go @@ -0,0 +1,18 @@ +package publicreport + +import ( + "net/http" + + "github.com/Gleipnir-Technology/nidus-sync/userfile" + "github.com/go-chi/chi/v5" +) + +// ServeImageByUUID reads an image with the given UUID from disk and writes it to the HTTP response +func getImageByUUID(w http.ResponseWriter, r *http.Request) { + uid := chi.URLParam(r, "uuid") + if uid == "" { + http.NotFound(w, r) + return + } + userfile.PublicImageFileToResponse(w, uid) +} diff --git a/public-report/routes.go b/public-report/routes.go index 8de97310..d843af5f 100644 --- a/public-report/routes.go +++ b/public-report/routes.go @@ -11,6 +11,7 @@ func Router() chi.Router { r.Get("/privacy", getPrivacy) r.Get("/robots.txt", getRobots) r.Get("/email/report/{report_id}/subscription-confirmation", getEmailReportSubscriptionConfirmation) + r.Get("/image/{uuid}", getImageByUUID) r.Get("/nuisance", getNuisance) r.Post("/nuisance-submit", postNuisance) r.Get("/nuisance-submit-complete", getNuisanceSubmitComplete) diff --git a/public-report/status.go b/public-report/status.go index 5d4c870e..524ec641 100644 --- a/public-report/status.go +++ b/public-report/status.go @@ -32,14 +32,19 @@ type Contact struct { Name string Phone string } +type Image struct { + URL string +} type Report struct { Address string + Comments string Created time.Time ID string + Images []Image Location string // GeoJSON Reporter Contact SiteOwner Contact - Updated time.Time + Type string } type ContentStatus struct { @@ -131,7 +136,6 @@ func contentFromNuisance(ctx context.Context, report_id string) (result ContentS result.Report.ID = report_id result.Report.Address = nuisance.Address result.Report.Created = nuisance.Created - result.Report.Updated = nuisance.Created result.Report.Reporter.Email = nuisance.ReporterEmail result.Report.Reporter.Name = nuisance.ReporterName result.Report.Reporter.Phone = nuisance.ReporterPhone @@ -162,14 +166,26 @@ func contentFromQuick(ctx context.Context, report_id string) (result ContentStat if err != nil { return result, fmt.Errorf("Failed to query nuisance %s: %w", report_id, err) } + + images, err := quick.Images().All(ctx, db.PGInstance.BobDB) + if err != nil { + return result, fmt.Errorf("Failed to get images %s: %w", report_id, err) + } + result.Report.ID = report_id result.Report.Address = quick.Address + result.Report.Comments = quick.Comments result.Report.Created = quick.Created - result.Report.Updated = quick.Created result.Report.Reporter.Email = quick.ReporterEmail result.Report.Reporter.Name = "-" result.Report.Reporter.Phone = quick.ReporterPhone + result.Report.Type = "Quick" + for _, image := range images { + result.Report.Images = append(result.Report.Images, Image{ + URL: fmt.Sprintf("https://%s/image/%s", config.RMODomain, image.StorageUUID), + }) + } type LocationGeoJSON struct { Location string } diff --git a/public-report/template/status-by-id.html b/public-report/template/status-by-id.html index 4235572f..9517ffce 100644 --- a/public-report/template/status-by-id.html +++ b/public-report/template/status-by-id.html @@ -51,12 +51,12 @@
- Created: - {{.Report.Created|timeSince}} + Type: + {{.Report.Type}}
- Last Updated: - July 17, 2023 - 2:45 PM + Created: + {{.Report.Created|timeSince}}
Next Step: @@ -94,8 +94,9 @@
+ -
+
Report Detail
@@ -103,6 +104,7 @@

Foo:Bar

+
@@ -116,6 +118,21 @@
+
+
+
Images
+
+
+ {{ if gt (len .Report.Images) 0 }} + {{ range .Report.Images }} + + {{ end }} + {{ else }} +

None

+ {{ end }} +
+
+
diff --git a/userfile/userfile.go b/userfile/userfile.go index 794450e0..aceea6ea 100644 --- a/userfile/userfile.go +++ b/userfile/userfile.go @@ -3,6 +3,7 @@ package userfile import ( "fmt" "io" + "net/http" "os" "github.com/Gleipnir-Technology/nidus-sync/config" @@ -78,6 +79,43 @@ func PublicImageFileContentWrite(uid uuid.UUID, body io.Reader) error { log.Info().Str("filepath", filepath).Msg("Saved public report image file content") return nil } + func PublicImageFileContentPathRaw(uid string) string { return fmt.Sprintf("%s/%s.raw", config.FilesDirectoryPublic, uid) } + +func PublicImageFileToResponse(w http.ResponseWriter, uid string) { + image_path := PublicImageFileContentPathRaw(uid) + + // Open the file + file, err := os.Open(image_path) + if err != nil { + if os.IsNotExist(err) { + http.Error(w, "Image not found", http.StatusNotFound) + } else { + http.Error(w, "Failed to retrieve image", http.StatusInternalServerError) + } + return + } + defer file.Close() + + // Get file info for Content-Length header + fileInfo, err := file.Stat() + if err != nil { + http.Error(w, "Failed to get image information", http.StatusInternalServerError) + return + } + + // Set appropriate headers + w.Header().Set("Content-Type", "image/png") + w.Header().Set("Content-Length", fmt.Sprintf("%d", fileInfo.Size())) + + // Copy file contents to response writer + _, err = io.Copy(w, file) + if err != nil { + // Note: At this point, we've already started writing the response, + // so we can't change the status code anymore. The best we can do + // is log the error and abandon the connection. + return + } +}