imagemagick with foreign characters - asp.net

ImageMagick doesnt seem to work with foreign characters. I use the following code
It works fine until a letter in the path or the file has a foreign character. How do i convert images to thumbs on my asp.net site? Is there a plug in or another app or version i may use?
Process app = new Process();
app.StartInfo.FileName = #"bin\convert.exe";
app.StartInfo.Arguments = string.Format(#"""{0}"" -resize ""{2}"" ""{1}""", file, newfile, param);
app.Start();
app.WaitForExit();

I would change the name of the file. You probably want to be doing some conversion of the file name anyway to help keep yourself safe from attacks embedded in a file's name. It's usually a bad idea to launch a subprocess with any string that a user can control. If you're catching uploaded files, move them to some new name before running convert.exe - like a name generated from a uuid, for instance.

A workaround is to change the filename to something ascii safe then rename/move it to the name/path you want with full unicode characters.

Answering this question may not be helpful for now but it might be useful for someone.
Converting file path to UTF-8 encoding worked for me.

Related

Sending Email Attachement Through Outlook in R with RDCOMClient

I'm Running a daily analysis that spits out a file I would like sent through my outlook Email. The code I used is featured here, and works wonderfully but the attachment part of it never works...
library(RDCOMClient)
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "gkinne#horizonmedia.com"
outMail[["subject"]] = "Bruh"
outMail[["body"]] = "Tester"
outMail[["Attachments"]]$Add("L:/Document.csv")
outMail$Send()
The original is here:
Sending email in R via outlook
The code works until the attachment part, and the email even sends, just with no Attachment. It spits this error out:
<checkErrorInfo> 80020009
No support for InterfaceSupportsErrorInfo
checkErrorInfo -2147352567
Error: Exception occurred.
Any Ideas?
Reverse the slashes and escape them.
The problem is that the path is being created in R, which prefers forward slashes (since the backslash is the escape character), but it's being interpreted by Outlook, which only takes backslashes.
For example, try adding an attachment to an Outlook email by pasting a path into the insert file dialogue, but change the backslashes to forward slashes. It doesn't accept it. And that's essentially what you're trying to do.
So reverse to make them backslashes, then add extra backslashes to each one to escape them. For example:
C:\\Users\\MyFiles\\Documents\\document.txt
R will strip out the escape characters and and pass a clean path to Outlook.
The answer that helped me was provided by David Arenburg in comments:
You need to specify a full path. Is L:/Document.csv a full path? Is L
a local driver or you mapped a network driver? If later is the case
you need to specify the actual network path.
Example: \\dfwcot\Home$\lando\bb8\2015-12-24 Daily Report.xlsx
The Add method of the Attachments class accepts four arguments. I'd suggest specifying them explicitly.
The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment. Make sure the file is accessible.
You need to do it like this
L:\\Document.csv
Worked for me. Use two backslashes.
I was also facing the same issue of "Error: Exception occurred".
But, in my case i was missing the naming convention of file. So, make sure that the file name must not be separated by SPACE and use delimiter as "-".
You can use the gsub() function to change "/" to double back slashes "\" in your path
Use this:
outMail[["Attachments"]]$Add(gsub("/","\\" ,"L:/Document.csv", fixed = TRUE))
The problem that our customer was facing was that they executed this operation one too many times.
In your example you are using the filename "L:/Document.csv" over and over again.
Outlook will create that attachment in one of the cache folders each time you try to generate this mail. The first one will be "Document.csv", the second one "Document (002).csv" etc...
We saw it running up to (799) and apparently that is the hard limit.
We had to clean out the temp folder in C:\Users<username>\AppData\Local\Microsoft\Windows\INetCache\Content.Outlook<random hex value> to make it work again.
Just double click an attachment in any mail and see where that temporary file shows up and check that directory.

Download an excel file as attachment using Qt browser

I am working on Qt browser(code taken from Qt Tab Browser Example). This Qt browser successfully downloads an image file, but not an excel/pdf file. I need to download an excel/pdf file as attachment on click of a button. Excel generation code at back-end uses PHPExcel to generate and finally saves it using 'php://output'.
On browser side, when I read through QNetworkReply's 'readAll()' function, some encoded string '��ࡱ�' gets printed and nothing gets saved in QFile object, totally empty file.
How do I get the desired excel file from this encoded string ?
Please, any help. It is Linux OS and I use LibreOffice if it matters.
Well, got the answer. I was printing QNetworkReply object's content on the console using reply->readAll() before reading it into the file and Qt documentation states that:
QNetworkReply is a sequential-access QIODevice, which means that once
data is read from the object, it no longer kept by the device.
Therefore, i ended up with an empty file. Found this when i printed the size of the 'reply' object. Also, found some eventLoop related help from here(Roozbeh's answer).
Hope it helps someone else as well. Thanks SO !

Overwrite an existing file programmatically

I have a QDialogBox where there is an option to upload a file.
I can upload files and save them to a folder. It works fine.
But if in case there is a file that already exists in the folder, I am not sure how to handle that scenario.
I want to warn the user that the file with same name already exists.
Is there a Windows API that I can use in this case? (because when we manually save an existing file, we get a warning, how can I use that?)
If someone can point me to that documentation, it will be great.
If you are using a QFileDialog, confirmOverwrite is activated by default, so, if getSaveFileName() returned a non-empty QString, then that means the user accepted to overwrite the file. Other way, you get an empty QString.
Then, you can check if the file exists, and remove it in that case, but you know that the user was Ok with that.
There is always a potential race condition when saving files. Checking to see if the file exists first is not safe, because some other process could create a file with the same name in between the check and when you actually write the file.
To avoid problems, the file must be opened with exclusive access, and in such a way that it immediately fails if it already exists.
If you want to do things properly, take a look at these two answers:
How do I create a file in python without overwriting an existing
file
Safely create a file if and only if it does not exist with
python
You can use QDir::entryList() to get the file names in a directory if you're not using a QFileDialog.
QDir dir("/path/to/directory");
QStringList fileNames = dir.entryList();
Then iterating through file names, you can see if there's a file with the same name. If you need it, I can give an example for that too. It'd be C++, but easily adaptable to Python.
Edit: Smasho just suggested that using QDir::exists() method. You can check if the file name exists in the directory with this method instead of iterating like I suggested.
if(dir.exists(uploadedFileName))

Best Way To Reference Current/Working Directory in VB.NET

I am after the one which is most used. A number of ones I have come across are:
CurDir
Environment.CurrentDirectory()
AppDomain.CurrentDomain.BaseDirectory
Application.StartupPath (this one doesn't work for me, missing a library?)
I am using it to save a file, for argument sake, "test.txt"
I may be oversimplifying, but if you want to save something in the folder where your app is running, just omit the path.
call MyObj.SaveTo("test.txt")

Getting extension of the file in FileUpload Control

At the moment i get file extension of the file like :
string fileExt = System.IO.Path.GetExtension(filUpload.FileName);
But if the user change the file extension of the file ( for example user could rename "test.txt" to "test.jpg" ), I can't get the real extension . What's the solution ?
You seem to be asking if you can identify file-type from its content.
Most solutions will indeed attempt the file extension, but there are too many different possible file types to be reliably identifiable.
Most approaches use the first several bytes of the file to determine what they are.
Here is one list, here another.
If you are only worried about text vs binary, see this SO question and answers.
See this SO answer for checking if a file is a JPG - this approach can be extended to use other file headers as in the first two links in this answer.
Whatever the user renames the file extension to, that is the real file extension.
You should never depend on the file extension to tell you what's in the file, since it can be renamed.
See "how can we check file types before uploading them in asp.net?"
There's no way to get the 'real' file extension - the file extension that you get from the filename is the real one. If file content is your concern, you can retrieve the content type using the .ContentType property and verify that it is a content type that you are expecting - eg. image/jpg.

Resources