@@ -24,6 +24,7 @@ import (
24
24
"net"
25
25
"net/http"
26
26
"net/url"
27
+ "os"
27
28
"os/user"
28
29
"path"
29
30
"path/filepath"
@@ -348,7 +349,80 @@ func GetViewMode(viewMode string) gateway.OpenInAppRequest_ViewMode {
348
349
return gateway .OpenInAppRequest_VIEW_MODE_READ_ONLY
349
350
case "write" :
350
351
return gateway .OpenInAppRequest_VIEW_MODE_READ_WRITE
352
+ case "preview" :
353
+ return gateway .OpenInAppRequest_VIEW_MODE_PREVIEW
351
354
default :
352
355
return gateway .OpenInAppRequest_VIEW_MODE_INVALID
353
356
}
354
357
}
358
+
359
+ // AppendPlainToOpaque adds a new key value pair as a plain string on the given opaque and returns it
360
+ func AppendPlainToOpaque (o * types.Opaque , key , value string ) * types.Opaque {
361
+ o = ensureOpaque (o )
362
+
363
+ o .Map [key ] = & types.OpaqueEntry {
364
+ Decoder : "plain" ,
365
+ Value : []byte (value ),
366
+ }
367
+ return o
368
+ }
369
+
370
+ // ReadPlainFromOpaque reads a plain string from the given opaque map
371
+ func ReadPlainFromOpaque (o * types.Opaque , key string ) string {
372
+ if o .GetMap () == nil {
373
+ return ""
374
+ }
375
+ if e , ok := o .Map [key ]; ok && e .Decoder == "plain" {
376
+ return string (e .Value )
377
+ }
378
+ return ""
379
+ }
380
+
381
+ // ExistsInOpaque returns true if the key exists in the opaque (ignoring the value)
382
+ func ExistsInOpaque (o * types.Opaque , key string ) bool {
383
+ if o .GetMap () == nil {
384
+ return false
385
+ }
386
+
387
+ _ , ok := o .Map [key ]
388
+ return ok
389
+ }
390
+
391
+ // MergeOpaques will merge the opaques. If a key exists in both opaques
392
+ // the values from the first opaque will be taken
393
+ func MergeOpaques (o * types.Opaque , p * types.Opaque ) * types.Opaque {
394
+ p = ensureOpaque (p )
395
+ for k , v := range o .GetMap () {
396
+ p .Map [k ] = v
397
+ }
398
+ return p
399
+ }
400
+
401
+ // ensures the opaque is initialized
402
+ func ensureOpaque (o * types.Opaque ) * types.Opaque {
403
+ if o == nil {
404
+ o = & types.Opaque {}
405
+ }
406
+ if o .Map == nil {
407
+ o .Map = map [string ]* types.OpaqueEntry {}
408
+ }
409
+ return o
410
+ }
411
+
412
+ // RemoveItem removes the given item, its children and all empty parent folders
413
+ func RemoveItem (path string ) error {
414
+ if err := os .RemoveAll (path ); err != nil {
415
+ return err
416
+ }
417
+
418
+ for {
419
+ path = filepath .Dir (path )
420
+ if err := os .Remove (path ); err != nil {
421
+ // remove will fail when the dir is not empty.
422
+ // We can exit in that case
423
+ return nil
424
+ }
425
+
426
+ }
427
+
428
+ }
0 commit comments