Mercurial > gemma
comparison pkg/geoserver/boot.go @ 4918:4a9a1e323e11 fairway-marks-import
First version of geo styles in ZIP files.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 13 Feb 2020 18:23:58 +0100 |
parents | 443867b548b5 |
children | 6129f7bb5b77 |
comparison
equal
deleted
inserted
replaced
4917:0098cfd602be | 4918:4a9a1e323e11 |
---|---|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> | 12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
13 | 13 |
14 package geoserver | 14 package geoserver |
15 | 15 |
16 import ( | 16 import ( |
17 "archive/zip" | |
17 "bytes" | 18 "bytes" |
18 "encoding/json" | 19 "encoding/json" |
19 "encoding/xml" | 20 "encoding/xml" |
20 "fmt" | 21 "fmt" |
21 "io" | 22 "io" |
476 stylePreprocessorsMu.Lock() | 477 stylePreprocessorsMu.Lock() |
477 defer stylePreprocessorsMu.Unlock() | 478 defer stylePreprocessorsMu.Unlock() |
478 return stylePreprocessors[name] | 479 return stylePreprocessors[name] |
479 } | 480 } |
480 | 481 |
482 func isZip(data []byte) bool { | |
483 if len(data) == 0 { | |
484 return false | |
485 } | |
486 _, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) | |
487 return err == nil | |
488 } | |
489 | |
481 func updateStyle(entry *models.IntEntry, create bool) error { | 490 func updateStyle(entry *models.IntEntry, create bool) error { |
482 | 491 |
483 log.Printf("info: creating style %s\n", entry.Name) | 492 log.Printf("info: creating style %s\n", entry.Name) |
484 | 493 |
485 // Try to load the style data. | 494 // Try to load the style data. |
486 data, err := entry.LoadStyle() | 495 binary, err := entry.LoadStyle() |
487 if err != nil { | 496 if err != nil { |
488 return err | 497 return err |
489 } | 498 } |
490 | 499 |
491 if processor := FindStylePreprocessor(entry.Name); processor != nil { | 500 zip := isZip(binary) |
492 if data, err = processor(data); err != nil { | 501 |
493 return err | 502 if !zip { // We only support templating for plain XML styles. |
494 } | 503 data := string(binary) |
504 if processor := FindStylePreprocessor(entry.Name); processor != nil { | |
505 if data, err = processor(data); err != nil { | |
506 return err | |
507 } | |
508 } | |
509 binary = []byte(data) | |
495 } | 510 } |
496 | 511 |
497 var ( | 512 var ( |
498 geoURL = config.GeoServerURL() | 513 geoURL = config.GeoServerURL() |
499 user = config.GeoServerUser() | 514 user = config.GeoServerUser() |
546 // Second upload data | 561 // Second upload data |
547 | 562 |
548 req, err := http.NewRequest( | 563 req, err := http.NewRequest( |
549 http.MethodPut, | 564 http.MethodPut, |
550 styleURL+"/"+url.PathEscape(entry.Name), | 565 styleURL+"/"+url.PathEscape(entry.Name), |
551 strings.NewReader(data)) | 566 bytes.NewReader(binary)) |
552 if err != nil { | 567 if err != nil { |
553 return err | 568 return err |
554 } | 569 } |
555 auth(req) | 570 auth(req) |
556 if isSymbologyEncoding(data) { | 571 switch { |
572 case zip: | |
573 asContentType(req, "application/zip") | |
574 case isSymbologyEncoding(binary): | |
557 asContentType(req, "application/vnd.ogc.se+xml") | 575 asContentType(req, "application/vnd.ogc.se+xml") |
558 } else { | 576 default: |
559 asContentType(req, "application/vnd.ogc.sld+xml") | 577 asContentType(req, "application/vnd.ogc.sld+xml") |
560 } | 578 } |
561 resp, err := http.DefaultClient.Do(req) | 579 resp, err := http.DefaultClient.Do(req) |
562 if err != nil { | 580 if err != nil { |
563 return err | 581 return err |
594 | 612 |
595 return nil | 613 return nil |
596 } | 614 } |
597 | 615 |
598 // isSymbologyEncoding tries to figure out if its plain SLD or SE. | 616 // isSymbologyEncoding tries to figure out if its plain SLD or SE. |
599 func isSymbologyEncoding(data string) bool { | 617 func isSymbologyEncoding(data []byte) bool { |
600 decoder := xml.NewDecoder(strings.NewReader(data)) | 618 decoder := xml.NewDecoder(bytes.NewReader(data)) |
601 decoder.CharsetReader = charset.NewReaderLabel | 619 decoder.CharsetReader = charset.NewReaderLabel |
602 | 620 |
603 for { | 621 for { |
604 tok, err := decoder.Token() | 622 tok, err := decoder.Token() |
605 switch { | 623 switch { |