* created service for sending google transcripts to gen ai * TP-45807| resolved bugs in drive service tests * TP-45807| unit tests for getting conversation data function * creating driveservice in app context and passing to rca service * modified the unit tests to accomodate driveservicemock * resolved merge conflicts * resolved merge conflicts
158 lines
3.8 KiB
Go
158 lines
3.8 KiB
Go
package util
|
|
|
|
import (
|
|
"archive/zip"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/spf13/viper"
|
|
"houston/logger"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func UnzipFile(zipFilePath string, destinationDirectoryPath string) error {
|
|
maxFileSize := viper.GetUint64("MAX_FILE_SIZE") * 1024 * 1024
|
|
zipFile, err := zip.OpenReader(zipFilePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func(zipFile *zip.ReadCloser) {
|
|
err := zipFile.Close()
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Error while closing zip file: %v", err))
|
|
}
|
|
}(zipFile)
|
|
|
|
if _, err := os.Stat(destinationDirectoryPath); os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
|
|
for _, file := range zipFile.File {
|
|
err := CheckFileSizeAndUncompress(file, destinationDirectoryPath, maxFileSize)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Error while unzipping file: %v", err))
|
|
}
|
|
}
|
|
|
|
logger.Info("Unzip completed successfully.")
|
|
return nil
|
|
}
|
|
|
|
func CheckFileSizeAndUncompress(file *zip.File, destinationDirectoryPath string, maxFileSize uint64) error {
|
|
if file.UncompressedSize64 > maxFileSize {
|
|
return errors.New(fmt.Sprintf("File with name %v is too big", file.Name))
|
|
}
|
|
zipFile, err := file.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func(zipFile io.ReadCloser) {
|
|
err := zipFile.Close()
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Error while closing zip file: %v", err))
|
|
}
|
|
}(zipFile)
|
|
|
|
destFilePath := filepath.Join(destinationDirectoryPath, file.Name)
|
|
destFile, err := os.Create(destFilePath)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Error while creating destination file: %v", err))
|
|
}
|
|
defer func(destFile *os.File) {
|
|
err := destFile.Close()
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Error while closing destination file: %v", err))
|
|
}
|
|
}(destFile)
|
|
|
|
// Check file size
|
|
if file.UncompressedSize64 > maxFileSize {
|
|
logger.Error(fmt.Sprintf("File with name %v is too big", file.Name))
|
|
}
|
|
|
|
_, err = io.Copy(destFile, io.Reader(zipFile))
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Error while copying file: %v", err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func CheckAndCreateDirectory(path string) error {
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
err := os.MkdirAll(path, os.ModePerm)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Error while creating directory :%v", err))
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func CreateFileWithName(path string, fileName string) (*os.File, error) {
|
|
err := CheckAndCreateDirectory(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
file, err := os.Create(filepath.Join(path, fileName))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return file, nil
|
|
}
|
|
|
|
func DeleteFile(path string) error {
|
|
err := os.Remove(path)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Error while deleting file :%v", err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func CloseFile(file *os.File) {
|
|
err := file.Close()
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Error while closing file: %v", err))
|
|
}
|
|
}
|
|
|
|
func WriteToFile(fileLocation string, fileName string, contentReader io.ReadCloser) error {
|
|
file, err := os.Create(fileLocation + "/" + fileName)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Error creating file with name %v : %v", fileName, err))
|
|
return err
|
|
}
|
|
_, err = io.Copy(file, contentReader)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Error copying file with name %v : %v", fileName, err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func DeleteDirectory(directoryPath string) {
|
|
err := os.RemoveAll(directoryPath)
|
|
if err != nil {
|
|
logger.Error(fmt.Sprintf("Error while deleting directory: %v", err))
|
|
}
|
|
}
|
|
|
|
func FetchFilesInDirectory(directoryPath string) ([]os.DirEntry, error) {
|
|
files, err := os.ReadDir(directoryPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return files, nil
|
|
}
|
|
|
|
func GetFile(directoryPath string, fileName string) (*os.File, error) {
|
|
fullPath := filepath.Join(directoryPath, fileName)
|
|
file, err := os.Open(fullPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return file, nil
|
|
}
|