Skip to content

Commit

Permalink
Binary file support (#4)
Browse files Browse the repository at this point in the history
* Support reading binary files

* Example for binary file support
  • Loading branch information
vvakar authored Jul 26, 2024
1 parent 2378215 commit 06cfb6b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/example-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ function readFile(file) {
console.log(file.path + ': ' + file.content);
}

function readBinaryFile(file) {
console.log(file.path + ' size: ' + file.binary.length + ' bytes');
}

function readDirectory(directory) {
console.log('directory :' + directory.path);
for (let i = 0; i < directory.content.length; i++) {
Expand All @@ -22,6 +26,9 @@ export default function () {
//console.log(JSON.stringify(file));
readFile(file);

let binaryFile = read.readBinaryFile(FILE_PATH);
readBinaryFile(binaryFile);

let directory = read.readDirectory(DIRECTORY_PATH);
//console.log(JSON.stringify(directory));
readDirectory(directory);
Expand Down
11 changes: 11 additions & 0 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type File struct {
Item
Path string
Content string
Binary []byte
}

func (f *File) GetPath() string {
Expand Down Expand Up @@ -79,3 +80,13 @@ func (*READ) ReadFile(path string) (File, error) {

return File{Path: path, Content: string(fileContent)}, nil
}

func (*READ) ReadBinaryFile(path string) (File, error) {
fileContent, readError := os.ReadFile(path)

if readError != nil {
return File{}, readError
}

return File{Path: path, Binary: fileContent}, nil
}

0 comments on commit 06cfb6b

Please sign in to comment.