Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function that returns a file in various size conversions (bytes, kb, mb) #2

Closed
marcobeltempo opened this issue Oct 12, 2017 · 5 comments

Comments

@marcobeltempo
Copy link
Owner

This function should be able to extract a filename from a path as well as calculate the file size.
Example: user/marco/mydata.txt
mydatat.txt 250 bytes

The node module filesize would be great for this implementation.

@MPierre9
Copy link
Collaborator

Hello, I'd like to take on this issue if possible

@marcobeltempo
Copy link
Owner Author

marcobeltempo commented Oct 12, 2017

@MPierre9 I ended up implementing the file size feature in the on line 23 of routes/index.js.
Maybe you could work on changing the output from 512 B to say 512 Bytes or even display the size in kb and bytes

@marcobeltempo marcobeltempo changed the title Function that returns a file size in bytes Function that returns a file in various size conversions (bytes, kb, mb) Oct 13, 2017
@MPierre9
Copy link
Collaborator

@marcobeltempo, managed to get the code to display Bytes instead of B as well as Kilobytes. Right now the function just returns the number however it can be changed to return a string saying 1024 Kilobytes for example. Here is the code:


     fs.stat(file, function (err, stats) {
        var fileStats = [
          'Ananalyzing file from ' + file,
          'Directory: ' + path.win32.dirname(file), //gets the files directroy 
          'Filename: ' + path.win32.basename(file), //gets file name
          'Extension: ' + path.win32.extname(file), //gets file extension
          'Size:' + stats.size + ' Bytes' + ' ' + getKilobytes(stats.size) + ' Kilobytes',        //gets file size and converst to readable bytes
          'File contents: ' + data                  //contents in the file
        ]

        res.render('index', {
          title: 'Fileside',
          stats: fileStats
        });
        //For debugging
        console.log("Directory: ", path.win32.dirname(file));
        console.log("Filename: ", path.win32.basename(file));
        console.log("Extension: ", path.win32.extname(file));
        console.log('Size:', filesize(stats["size"]));
        console.log("File contents: ", data);
      });
    }

  });

});

function getKilobytes(bytes) {
  
  var convertToKiloBytes = bytes / 1024
  var kilobytes = convertToKiloBytes.toFixed(2)

  return kilobytes;
}

new bytes

@marcobeltempo
Copy link
Owner Author

Nice work! @MPierre9 I think we should leave it the way it is returning the size in KB.
Submit a pull request and I'll look it over

@marcobeltempo
Copy link
Owner Author

marcobeltempo commented Oct 19, 2017

Pull Request #6 - Added output of file size in kilobytes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants