Changing extension changes the mimetype in multer - mime-types

I was following a youtube tutorial on file upload using multer. From the video, changing the extension doesn't change the file's mimetype. Only that upon replicating the process, the original file was a json file with the following information,
{
fieldname: 'images',
originalname: 'budg.ods',
encoding: '7bit',
mimetype: 'application/vnd.oasis.opendocument.spreadsheet',
destination: './uploads',
filename: '597077caac33d200e499f1b1fed1b7d7',
path: 'uploads/597077caac33d200e499f1b1fed1b7d7',
size: 12213
}
On changing the extension of the same file, I got the following
{
originalname: 'budg.png',
encoding: '7bit',
mimetype: 'image/png',
filename: 'c05f55e6168dd1a22cfd1fa29e69ed1c',
size: 12213,
...
}
Is the video's implementation the right way of checking a file's mimetype, regardless of the extension, and if not, what's a better alternative?

Related

Upload a file using React and Express

i want to upload a zip file from a client to my server. On my client side i have adapt the code from this site. In my express server i notice that my request is
req.files: { file:
{ fieldName: 'file',
originalFilename: 'myZIP.zip',
path: '/tmp/myPath',
headers:
{ .....},
size: 11757,
name: 'myZIP.zip',
type: 'application/zip' } }
Is there any way to upload my file without give the path of the file /tmp/... ? Or this is the only way that uploads works in general?

Is it possible to rewrite the route of a static .txt file in Next.js

I have a Next.js application which has a robots-staging.txt file in the root of the public folder. I'm looking to add this to the rewrites function in next.config. This is what I have
async rewrites() {
const rewriteList = [
{
source: '/robots-staging.txt',
destination: '/robots.txt',
},
];
return rewriteList;
},
My initial expectation was that when I hit localhost:3000/robots.txt this would serve the staging file, however it's not working. Am I missing something?
If I understood correctly that you want to proxy /robots.txt to /robots-staging.txt, you need to make the latter the destination and not the source.
Besides that, I've experienced the same issue, and I'm not sure if this is a bug or a feature, but I found that using absolute paths/URLs works as a workaround as relative paths seem to be interpreted as pages:
async rewrites() {
{
source: "/robots.txt",
destination:
process.env.NODE_ENV === "development"
? "http://localhost:3000/robots-staging.txt"
: "https://YOUR_URL/robots-staging.txt",
},
];
},

FileError { kind: IOError(Os { code: 2, kind: NotFound, message: "No such file or directory" }), path: "Some(\"assets/images/tiles/air.png\")" }'

Tell me if I need to provide more info, I am rather new here. Here is the code:
let mut image_paths = vec![];
let mut images = vec![];
let mut image_path = "assets/images/tiles/";
for img in glob(format!("{}*.png",image_path).as_str()).unwrap() {
match img {
Ok(path) =>{
image_paths.push(format!("{:?}",path.to_str()));
},
Err(e) => continue,
}
}
for i in &image_paths {
images.push(load_texture(i).await.unwrap());
}
But I get this error:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: FileError {
kind: IOError(Os { code: 2, kind: NotFound, message: "No such file or directory" }),
path: "Some(\"assets/images/tiles/air.png\")" }', src/main.rs:71:43
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Which is really bizarre, because I can confirm that 'air.png' is in the 'tiles' folder, I have been able to load the image simply by doing load_texture(), but I don't understand why I cannot load it now.
Path::to_str returns an Option<&str>. You're applying debug formatting ({:?}) on that, then adding the result to image_paths. I'm pretty sure your file path isn't supposed to start with Some(.
When you work with paths in Rust, you generally want to use Path or PathBuf unless you need to display the path. Check to see if load_texture could receive a &Path or a PathBuf. If not, then you need to unwrap the Option or deal with the None in whatever way is appropriate for your situation.
image_paths.push(path.to_str().unwrap());
// or if you need a `String` instead of a `&str`:
image_paths.push(path.to_str().unwrap().to_string());

angular-csv-import not working on ionic

I am trying to use angular-csv-import on ionic. all works greate on chrome but when I am trying to run it on android nothing is happens...file manager to upload the file isnt working....what can be done?
my code is:
template:
<ng-csv-import content="csv.content"
header="csv.header"
separator="csv.separator"
result="csv.result"
accept="csv.accept">
</ng-csv-import>
controller:
$scope.csv = {
content: null,
header: true,
headerVisible: true,
separator: ',',
separatorVisible: true,
result: null,
encoding: 'ISO-8859-1',
encodingVisible: true,
accept: ".csv"
};
I find 2 ways to do it.
One way to save the document from google shits and get plugin to parse it to json, then use ng to download parsed json. Or the other way to use xlsx parser

i18next is not loading the translation file

I am working on some Backbone based project where i am using i18next for locales.
Following is my app.js code:
/*
This file is used to initialize your application.
*/
require(['i18n','application','handlebars_Helpers'], function(i18n, Application) {
i18n.init({
lng: 'en',
debug: true,
fallbackLng: false,
load:'unspecific',
resGetPath: "locales/__lng__/__ns__.json",
ns: {
namespaces: ['translation']
}
});
(new Application()).initialize();
});
Translation file:
{
"loginModule": {
"signin": "Sign In"
}
}
Following is my helper file:
/**
* Set of generic handlebars helpers
*/
define(['i18n'], function(i18n) {
/**
* This helper provides i18Next in templates
*
*
* Usage: span {{t "my.key" }}
*/
Handlebars.registerHelper('t', function(i18n_key) {
var result = i18n.t(i18n_key);
return new Handlebars.SafeString(result);
});
return Handlebars;
});
When i am loading my page through localhost it shows me following message in console:
currentLng set to: en i18n.js:490
GET http://localhost:8000/locales/en/translation.json?_=1374495189376 404 (Not Found) i18n.js:376
failed loading: locales/en/translation.json
Don't understand what i am missing? or why this error is show?
In which folder do you store translations file? Default behavior for i18n is, that it tries to find localization file in specific path: /locales/{lang-code}/{namespace}.json
If you keep file in root, try to change initialization code to following:
i18n.init({
lang: 'en',
debug: true,
fallbackLng: false,
load:'unspecific',
resGetPath: "__ns__-__lng__.json",
ns: {
namespaces: ['translation'],
defaultNs: 'translation'
}
});
This will try to load file from following url: http://localhost:8000/translation-en.json
Basically, try to check location of translations file, name of translation file and construct 'regGenPath' accordingly, more info can be found in i18n documentation http://i18next.com/node/pages/doc_init.html

Resources