github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/encrypt/store_publication.go (about) 1 // Copyright 2021 Readium Foundation. All rights reserved. 2 // Use of this source code is governed by a BSD-style license 3 // that can be found in the LICENSE file exposed on Github (readium) in the project repository. 4 5 package encrypt 6 7 import ( 8 "errors" 9 "os" 10 "strings" 11 12 "github.com/readium/readium-lcp-server/storage" 13 ) 14 15 // StoreS3Publication stores an encrypted file into its definitive storage. 16 // Only called for S3 buckets. 17 func StoreS3Publication(inputPath, storagePath, name string) error { 18 19 s3Split := strings.Split(storagePath, ":") 20 21 s3conf := storage.S3Config{} 22 s3conf.Region = s3Split[1] 23 s3conf.Bucket = s3Split[2] 24 25 var store storage.Store 26 // init the S3 storage 27 store, err := storage.S3(s3conf) 28 if err != nil { 29 return errors.New("could not init the S3 storage") 30 } 31 32 // open the encrypted file, defer its deletion 33 file, err := os.Open(inputPath) 34 if err != nil { 35 return err 36 } 37 defer cleanupTempFile(file) 38 39 // add the file to the storage with the name passed as parameter 40 _, err = store.Add(name, file) 41 if err != nil { 42 return err 43 } 44 return nil 45 } 46 47 // cleanupTempFile closes and deletes a temporary file 48 func cleanupTempFile(f *os.File) { 49 if f == nil { 50 return 51 } 52 f.Close() 53 os.Remove(f.Name()) 54 }