Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 513 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Node.js get file extension

#11
import extname in order to return the extension the file:
```
import { extname } from 'path';
extname(file.originalname);
```

where file is the file 'name' of form
Reply

#12
You can use [path.parse(path)][1], for example

```JavaScript
const path = require('path');
const { ext } = path.parse('/home/user/dir/file.txt');
```

[1]:

[To see links please register here]

Reply

#13
Try this one

```js
const path = require('path');

function getExt(str) {
const basename = path.basename(str);
const firstDot = basename.indexOf('.');
const lastDot = basename.lastIndexOf('.');
const extname = path.extname(basename).replace(/(\.[a-z0-9]+).*/i, '$1');

if (firstDot === lastDot) {
return extname;
}

return basename.slice(firstDot, lastDot) + extname;
}

// all are `.gz`
console.log(getExt('/home/charlike/bar/file.gz'));
console.log(getExt('/home/charlike/bar/file.gz~'));
console.log(getExt('/home/charlike/bar/file.gz+cdf2'));
console.log(getExt('/home/charlike/bar/file.gz?quz=zaz'));

// all are `.tar.gz`
console.log(getExt('/home/charlike/bar/file.tar.gz'));
console.log(getExt('/home/charlike/bar/file.tar.gz~'));
console.log(getExt('/home/charlike/bar/file.tar.gz+cdf2'));
console.log(getExt('/home/charlike/bar/file.tar.gz?quz=zaz'));

```
Reply

#14
A one liner which extends `String.prototype`:

```
Object.defineProperty(String.prototype, "ext", {get: function(x) {return this.split('.').pop()}})
str = 'fox.fbx';
str.ext
```

Result:

[![enter image description here][1]][1]


[1]:
Reply

#15
I believe you can do the following to get the extension of a file name.

```javascript
var path = require('path')

path.extname('index.html')
// returns
'.html'
```

If you would like to get all extensions in a file name (e.g. `filename.css.gz` => `css.gz`), try this:

```javascript
const ext = 'filename.css.gz'
.split('.')
.filter(Boolean) // removes empty extensions (e.g. `filename...txt`)
.slice(1)
.join('.')

console.log(ext) // prints 'css.gz'
```
Reply

#16
```js
const path = require('path');

function getExt(str) {
const basename = path
.basename(str)
// Patch: for hidden files
// Removes all dots at the beginning of a line
.replace(/^(\.+)/i, '');

const firstDot = basename.indexOf('.');
const lastDot = basename.lastIndexOf('.');
const extname = path.extname(basename).replace(/(\.[a-z0-9]+).*/i, '$1');

if (firstDot === lastDot) {
return extname;
}

return basename.slice(firstDot, lastDot) + extname;
}
```
```js
const files = [
'/home/charlike/bar/.hidden.tar.gz~', // ".tar.gz"
'/home/charlike/bar/file.tar.gz~', // ".tar.gz"
'/home/charlike/bar/file.tar.gz+cdf2', // ".tar.gz"
'/home/charlike/bar/file.tar.gz?quz=zaz', // ".tar.gz"
];

const fileAndExt = files.map((file) => [ file, getExt(file) ]);

console.log(JSON.stringify(fileAndExt, null, 2));
```
Reply

#17
Using `path` is not really reliable as it depends on extension name being in the file name, which is not always the case.

You should use [file-type package][1] it detects extension by checking the magic number of the buffer, rather than depending on name of the file which not always have extension in it.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through