Nodejs: Detect file type without extension - node.js

I'm looking to remotely download and detect a file from a website, like this
http://examplewebsite.com/100/download
When viewing in my browser, this automatically downloads as the appropriate file type, 100.pdf, but sometimes it can be a .xls or .doc file. etc.
Looking at libraries available, like file-type, it only works if you already have the extension
Is this possible?

If you have the url, you can split by '.' and select the last element of your list.

The file-type library you linked in your question actually checks the source of the file to guess. It doesn't use the file extension at all.

Related

How to block the executable file upload in web server

I am working on a web app project to block all the file executable from file upload.
Example: user can upload, txt, png, image and video files and not any executable scripts like, Perl, Python, exe, PHP, .so, .sh files.
If it is a PHP file, then I strstr for "<?php" tag, If this tag is present, then it is PHP file. How can we find the same for other script/executable files?
Edit: Some time hackers will upload the malicious files using .png or .jpg extn, so what is the pattern to check inside the files?
Rather than making your own checks you make use of an existing library and you block everything that does not register as a desired format.
Most such libraries guess the content type and encoding of a file by looking for certain signatures or magic byte sequences at specific positions within the file.
Other libraries may be more specialised and will for example only identify image or video formats.
https://www.php.net/manual/en/intro.fileinfo.php
https://github.com/ahupp/python-magic
https://docs.python.org/3/library/imghdr.html
The file programme is a command line tool for identification of file types.
After the first pass where you identify and accept only the desired file formats you should then make all files that are not rejected go through an antivirus scanner.
Depending on you use cases you may decide to strip the original file name extension and/or even the complete file name that was provided during the upload and assign the mime-type that was detected rather than rely on user provided properties.

How does File Explorer open files?

It's maybe stupid question, but what happens behind the scenes when i double click the word app, or .exe app?
For 'Non-Developer' it just opens the file in right environment after doubleclicking. But I would like to know, how is it done, how can the file manager know what to open? (.docx in word, .txt in texteditor,etc...)
+ I would like to know how can I do that in Node.js, is it the best way to use child_process and if statements for every suffixes?
There is a file association to tell Windows how to treat a certain file extension. You can type assoc in a Windows Command Prompt to see them. As an example:
C:\test>assoc .txt
.txt=txtfile
So Windows knows now, that the file with the extension .txt is a txtfile.
ftype defines, how that filetype is to be handled:
C:\test>ftype txtfile
txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1
So whenever you doubleclick on a file, Windows checks if it is associated with any filetype. Then it looks up, how to handle that filetype and executes that command (in the example above, it opens Notepad with the filename as a parameter).
Both assoc and ftype are able to change the settings (for example to open .txt files with another editor). But if you try that, do yourself a favor and note the original settings, so you are able to revert your changes when needed.
The changes can also be done directly in the registry (not recommended, when you are not experienced in handling the registry)
For Node.js, you can use child_process to shell out to the start command which does similar things as the File Explorer.
E.g. start some/path/to/file.docx will open that file in the default program associated with the format.

why file extenssions are created?

my friends
I have a question about why file extensions are created?
I found a quote on Wikipedia
"They are commonly used to imply information about the way data might be stored in the file"
what does it mean?
File extension is an identifier which tell the operating system what kind of data and file type they are working with and what associated program opens the file.
if u have an .apk extension file, system can easily recognize it as an application file. If it is an mp4, means it's some kind of multimedia file and can be operated with multimedia applications.
They are commonly used to imply information about the way data might be stored in the file. A normal text editor uses .txt as extension when an html uses extension .html These two files stores data differently.

How to edit a .sx file

I am trying to edit an .sx file associated with this addon for microsoft test manager: Test Scribe
But if I make any change to the file in any editor test manager crashes. This should work since other people have said this is how they fixed the plugin not handling certain characters.
When I open the file in notepad++ it looks almost like an archive or something. Any ideas?
Figured it out, turns out it is using zip so you can open it and edit the files inside.

How can I get a file or directory name from a file open dialog in PyQt4?

I am writing a PyQt4 application and one of the file types I wish to open is an Esri Grid format which, rather unusually, is a directory. I also wish to open other GIS filetypes that are just files (e.g. geotiffs). I can open these filetypes OK with the GDAL library by passing either a file or directory name and GDAL figures it out.
The problem I have is making the GUI. I want to open a file open dialog and get either a file name, or directory name. The problem is that the file dialog won't let me choose a directory - only files. I need the dialog to return a path to either. I've tried it on Mac and Linux.
I know PySide has a method called getExistingDirectory
http://pyside.github.io/docs/pyside/PySide/QtGui/QFileDialog.html
PyQt is basically the same, so it should have a similar method. http://pyqt.sourceforge.net/Docs/PyQt4/qfiledialog.html It is in the static methods section.
I think I've cracked it. This snippet tests the functionality I need:
dlg=QtGui.QFileDialog()
dlg.setFileMode(QtGui.QFileDialog.AnyFile);
e=dlg.exec_()
print dlg.selectedFiles()[0]
The solution was to set the file mode to 'AnyFile'. This allows the file dialog to return both directory and file names.

Resources