How to get the file-name before downloading the file - c++

I am trying to download a binary file from a http: server. I am using the functions InternetOpenUrl() and then InternetReadFile() to download the file. Is it possible to know the file name before downloading?
What I am doing now to get the file name is- Once the download is complete, using GetFileVersionInfo() and from the buffer i am getting the OrginalFilename, then renaming the file to the OrginalFilename.
Is there any other way to get the file name before downloading?
Thanks
Vinod

Look at HttpQueryInfo. Look at the Content-Type and Content-Disposition headers.
You may have to use HTTP_QUERY_CUSTOM to get raw content-type if it just returns e.g. "text/plain".
To get all the headers (and thereby work out which one contains the information you want) you can use HTTP_QUERY_RAW_HEADERS_CRLF.

Related

Always serve unversioned files raw?

I'm serving unversioned files via fossil's uv function. Now, this works fine for files without file extension and for archives. But I need to serve a .txt file. The problem now is that it gets delivered as a HTML page including the fossil web layout around it.
Is there a way to tell fossil to not do that, and instead deliver it as a raw .txt file?
You can specify a mimetype parameter on the URL. For example, mimetype=application/octet-stream will cause it to be offered as download.
For example, instead of https://www.fossil-scm.org/index.html/uv/download.html, you’d put https://www.fossil-scm.org/index.html/uv/download.html?mimetype=application/octet-stream.
Fossil reacts to the following mimetypes by putting headers around them:
text/x-fossil-wiki
text/x-markdown
text/html
text/plain
Unfortunately, all other mimetypes appear to lead to the browser downloading the unversioned file instead of displaying it.
If that's a problem, you could try a mimetype of text with no suffix.
Otherwise, you can post on Fossil's support forum. Either as a question or as a feature request. :-)

Partial .gz file written by log4j2

I have the following log configuration.
<RollingRandomAccessFile name="RollingReqAppender"
fileName="/usr/test/req.log"
filePattern="/usr/test/req.log.%d{yyyy-MM-dd-HH}.%i.gz"
immediateFlush="true" append="true">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS Z}%m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="500 MB"/>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingRandomAccessFile>
When the log is rolled over (based on time or size), it is gzipped. Gzipping the file takes some time (seconds to minutes depending on load). The problem is that the file is gzipped in place, and while it is being gzipped, the partially gzipped file is stored in the same folder with the same name as the final gzip file. e.g.: /usr/test/req.log.2017-01-03-02.1.gz
This causes problems for the file's consumer, which attempts to process the partial file. I want to be able to distinguish between a partial gzipped file and a completed gzipped file. For example, I would like to setup a configuration that while the file is being gzipped, it is written with a different name e.g.: /usr/test/req.log.2017-01-03-02.1.temp.gz and once it is completely gzipped the name is changed to the final name /usr/test/req.log.2017-01-03-02.1.gz
How can I achieve this?
Log4j2 currently does not behave like you describe. So there is no configuration to make this happen.
Please raise a feature request on the Log4j2 JIRA issue tracker. The fastest way to achieve this is to provide a patch with unit test.
This feature was added to log4j2 version 2.9.0 https://issues.apache.org/jira/browse/LOG4J2-1766

How does one determine the filetype on an AWS S3 hosted file without the extension?

As an example, I'm currently uploading items directly to an S3 bucket using a form. While I was testing, I didn't specify any expected filenames or extensions.
I uploaded a .png which produced this direct link:
https://s3-us-west-2.amazonaws.com/easyhighlighting2/2015-07-271438019663927upload94788
When I place this inside an img tag, it displays on a web page properly.
My question is, without an extension, how would my browser know what type of file it's loading? Inside the bucket, the file's metadata isn't even filled out.
Is there any way to get that file extension, programmatically?
I'm ready to try any clientside methods available; my server-side language is ColdFusion which is somewhat limiting, but I'm open to suggestions for that as well.
Okay, so after some more extensive digging, I found a method of retrieving the file's type that was only added since CF10 was released; that would explain the lack of documentation.
The answer lies in the FileGetMimeType function.
<cfset someVar = "https://s3-us-west-2.amazonaws.com/easyhighlighting2/2015-07-271438019663927upload94788">
<cfset FileType = FileGetMimeType(someVar)>
<cfoutput>#FileType#</cfoutput>
This code would output image/png - which is correct and has worked for every filetype I have tested thus far.
I'm surprised this kind of question hasn't popped up before, but this appears to be the best answer, at least for users of CFML.
Edit:
ColdFusion accomplishes this by either reading the contents of a file, or by trusting its extension. An implicit attribute, 'strict', is used in this function. If true, it reads the file's contents. If false, it uses the provided extension.
True is the default.
Link:
https://wikidocs.adobe.com/wiki/display/coldfusionen/FileGetMimeType
Check the Content-Type HTTP response header returned by Amazon S3.
For example, curl -I https://s3.amazonaws.com/path/to/file fetches only the headers.

rails4 upload file extension error ActionDispatch::Http::UploadedFile

Guy
now i want to upload file with rails 4
my problem now i can't check the file extension before upload it
Note : I can upload the file well but i want to get the file kind before upload it
because i need the extension in another step in my App.
I'm tried to use the commands
File.extname(params[:Upload])
but always got the error
can't convert ActionDispatch::Http::UploadedFile into String
also how i can get the file base name before upload it ??
when i tryed to use
File.basename(params[:Upload])
i got the same error
can't convert ActionDispatch::Http::UploadedFile into String
also when i tried to convert the name to Sting i don't get any thing
That's because File.extname expects a string file name, but an uploaded file (your params[:upload] is an object, it's an instance of the ActionDispatch::Http::UploadedFile class (kind of a temporary file)
To fix the problem you need to call the path property on your params[:upload] object, kind of like that
File.extname(params[:Upload].path)
Btw, if you're trying to get the type of the uploaded file, I'd encourage you to check for the params[:Upload].content_type instead, it's harder to spoof
You can use this:
params[:Upload].original_filename.split('.').last
The original_filename contains full filename with extension.
so you split it based on '.' and last index will contain the file extension.
For Example:
"my_file.doc.pdf".split('.').last # => 'pdf'
You can check this for more info ActionDispatch::Http::UploadedFile.

Is to possible to find filename from http headers

Usually when downloading files, suppose using QNetworkAccessManager, the filename is not present at the end of link. How to get proper file names in that case. Even if link doesn't contain a hint of name, firefox always downloads the file with its proper name and extension. We can get a hint of extention using mime-types but what about file names.
Yes. It's the Content-Disposition: attachment; filename=<file name.ext> header. There's a strong suggestion to set the content type to application/octet-stream so browsers and their plugins are not tempted to open it instead.