Skip to content

Commit

Permalink
Merge pull request #1 from ARandell-ClaireIT/fork
Browse files Browse the repository at this point in the history
WriteTo function takes an io.Writer interface
  • Loading branch information
AlexJarrah authored Jul 5, 2024
2 parents 7f3b914 + 7e26e12 commit 810142c
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,32 @@ import (
"os"
)

// Updates the specified ODS file with the provided content
// Write Updates the specified ODS file with the provided content
func Write(filepath string, ods ODS, files *zip.ReadCloser) error {
buf := new(bytes.Buffer)

err := WriteTo(buf, ods, files)
if err != nil {
return err
}

outputFile, err := os.Create(filepath)
if err != nil {
return fmt.Errorf("error creating output file: %v", err)
}
defer outputFile.Close()

// Write the new ODS file
_, err = outputFile.Write(buf.Bytes())
if err != nil {
return fmt.Errorf("error writing to file: %v", err)
}

return nil
}

// WriteTo Updates the specified io.Writer with the provided content
func WriteTo(writer io.Writer, ods ODS, files *zip.ReadCloser) error {
// Translate the content for XML compatibility
odsMarshal, err := translate(ods)
if err != nil {
Expand Down Expand Up @@ -45,8 +69,7 @@ func Write(filepath string, ods ODS, files *zip.ReadCloser) error {
}

// Create a new zip archive in memory
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
w := zip.NewWriter(writer)

// Add files to the archive, updating "content.xml" with the modified data
for _, file := range files.File {
Expand Down Expand Up @@ -106,18 +129,6 @@ func Write(filepath string, ods ODS, files *zip.ReadCloser) error {
return fmt.Errorf("error closing archive: %v", err)
}

outputFile, err := os.Create(filepath)
if err != nil {
return fmt.Errorf("error creating output file: %v", err)
}
defer outputFile.Close()

// Write the new ODS file
_, err = outputFile.Write(buf.Bytes())
if err != nil {
return fmt.Errorf("error writing to file: %v", err)
}

return nil
}

Expand Down

0 comments on commit 810142c

Please sign in to comment.