// Very simple module to download a file using WinInet API (CaptainCPS-X)
#include "burner.h"
#include <wininet.h>
// TODO:
// - Verification of PNG headers instead of checking for regular errors on transfered files
// - Checking of more specific directory to download images form, Ex. flyers/ titles/ cabinets/ etc.
// right now the directory is specified in the server URL.
int FileDownload(TCHAR* szLocalFilePath, TCHAR* szFile)
{
HINTERNET url, open;
int errorCode = 0;
DWORD buffer = 0;
char *content;
FILE* dlPointer = NULL;
TCHAR szFILESERVER1[512] = _T("http://fightercore.plesk3.freepgs.com/files/neosource/fba/support/previews/");
TCHAR szFinalURL[512] = _T("");
// Prepare final URL for current download
_stprintf(szFinalURL, _T("%s%s"), szFILESERVER1, szFile);
// Start initialization for this connection
open = InternetOpen(_T("FBA_Download"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
// Connect to the final URL to start transfer later
url = InternetOpenUrl(open, szFinalURL, NULL, 0, INTERNET_FLAG_NO_AUTO_REDIRECT, 0);
TCHAR szStatusCode[32];
DWORD dwStatusCodeSize = 32;
if (!HttpQueryInfo(url,HTTP_QUERY_STATUS_CODE,szStatusCode, &dwStatusCodeSize, NULL))
{
// server not available so error here
}
else
{
long nStatusCode = _ttol(szStatusCode);
if (nStatusCode == HTTP_STATUS_OK)
{
// Create a new empty file for writing binary data
dlPointer = _tfopen(szLocalFilePath, _T("wb"));
TCHAR szContentSize[32];
DWORD dwContentSizeSize = 32;
if (HttpQueryInfo(url,HTTP_QUERY_CONTENT_LENGTH,szContentSize, &dwContentSizeSize, NULL))
{
long nContentSize = _ttol(szContentSize);
content = (char*)malloc(nContentSize);
// Transfer the file contents and write them to the created file
while(InternetReadFile(url, content, sizeof(content), &buffer) && buffer != 0) {
fwrite(content, 1, buffer, dlPointer);
content[buffer] = '\0';
}
// Done with file writing so close the file pointer
fclose(dlPointer);
errorCode=1;
}
}
else
{
// not code 200 so error here
errorCode= nStatusCode;
}
}
if (content!=NULL)
{
free(content);
}
buffer = 0;
InternetCloseHandle(url);
InternetCloseHandle(open);
return errorCode; // All good!
}
Basic http code checking. Probably not full thought through everything here but it seems to work ok-ish.