filemanagement

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2024 License: MIT-0 Imports: 11 Imported by: 2

README

File Management (v0.1.2)

Go Reference

Common functionalities in managing and handling files in GO.

The functions are:

Check
  • FileExist: Checks if the file exists or not
Convert
  • ByteToBase64: Converts byte array to Base 64
CSV
  • ReadCsvFile: Reads a CSV file and converts the contents to an array of arrays of strings
  • WriteCsvFile: Receives the content of the CSV file as a slice of string slice and writes it to file
Download
  • DownloadFileToDisk: Downloads a file and writes it to the disk
  • DownloadFileToBytes: Downloads a file and converts it to []byte
  • DownloadFileToBase64: Downloads a file and converts to base 64
  • DownloadFileToFile: Downloads a file and converts to a temporary os.File
File System
  • ReadFileToBase64NoError: Reads a file and convert it to base 64
  • GetDirFilesFlat: Creates a flat array containing the path to all of the files of a folder and its nested folders.
  • FindFirstFileInFolderContainingText: Returns the first file in a folder (and its nested folders) that has a text
Generate
  • CreateDirIfNotExists: Creates a directory if doesn't exist
Manipulation
  • ReplaceTextInFile: Replaces a text in a file
  • InsertTextInFile: Inserts a text before or after a landmark.
  • AppendToFile: Adds a text to the bottom of a file
Movement
  • Copy: Copies a file
  • CopyDirectory: Copies a directory and its nested folders
  • CopySymLink: Copies a sym link

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendToFile

func AppendToFile(filePath string, newContent string) error

Adds new text to the bottom of the file and writes it back to the disk

func ByteToBase64

func ByteToBase64(b []byte) string

Converts byte array to Base 64

func Copy

func Copy(srcFile, dstFile string) error

Copies a file to a destination

func CopyDirectory

func CopyDirectory(scrDir string, dest string, ignoreList []string) error

Copies a directory and its nested folders

func CopySymLink(source, dest string) error

Copies a sym link

func CreateDirIfNotExists

func CreateDirIfNotExists(dir string, perm os.FileMode) error

Creates a directory if doesn't exist

func DownloadFileToBase64

func DownloadFileToBase64(fileURL string, includeDataPrefix bool) (string, error)

Downloads the file from `fileURL` and converts it into base64

The base64 can have a prefix, e.g. `data:image/jpeg;base64,`

This prefix can be omitted or included using `includeDataPrefix`

func DownloadFileToBytes

func DownloadFileToBytes(fileURL string) ([]byte, error)

Downloads the file from `fileURL` and converts it into bytes

func DownloadFileToDisk

func DownloadFileToDisk(diskFilePath string, diskFileName, fileURL string, chmodPermission *fs.FileMode) (string, string, error)

Downloads the file from `fileURL` and writes to the disk

The new file will be named as `diskFileName` and will be saved to `diskFilePath`

If the permission of the file should be changed, it can be passes as `chmodPermission`, e.g. 0777 The return values are -> (file name, file path, error)

func DownloadFileToFile added in v0.1.2

func DownloadFileToFile(fileURL string) (*os.File, error)

Downloads a file from `fileURL` and converts it into a temporary `os.File`

func FileExists

func FileExists(filePath string) bool

Checks if the file exists or not

func FindFirstFileInFolderContainingText

func FindFirstFileInFolderContainingText(folderPath string, subStr string) (string, error)

Finds the first file in a folder (and its children) that contains a text

func GetDirFilesFlat

func GetDirFilesFlat(scrDir string) ([]string, error)

Collects all of the files of the given directory and its nested folders.

The paths to all of the files will return in a flat array

func InsertTextInFile

func InsertTextInFile(filePath string, toBeInserted string, landmark string, after bool) error

Inserts a text in a file after a landmark text

func ReadCsvFile

func ReadCsvFile(filePath string) ([][]string, error)

Reads a CSV files and converts to an array of array of strings

func ReadFileToBase64NoError

func ReadFileToBase64NoError(filePath string) string

Reads a file from the disk and converts to the base64

This function does not return any error. if any errors, it returns an empty string

func ReplaceTextInFile

func ReplaceTextInFile(filePath string, oldText string, newText string) error

Replaces a certain text in a file and writes it back to the disk

func WriteCsvFile added in v0.1.2

func WriteCsvFile(content [][]string, filename string) error

Receives the content of the CSV file as a slice of string slice and writes it to file

Types

type NotFoundError

type NotFoundError struct {
	Target string
}

func (*NotFoundError) Error

func (m *NotFoundError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL