It takes your desired file, downloads it if it has to, and loads the file(path) into a variable for later use. Default directory is A_AppsData\ScriptName\ResourceType\Resource. Very simple, very handy. Improvements or suggestions are welcome. I really like storing the data in AppsData: That way all data is "hidden" but accessible regardless of how the user moves the main exe or ahk. However, keep in mind that you shouldn't clog it up with heavy files.
Function:
/*
< remoteResource() > (function)
Version: 0.81
Author: Simon Strålberg [sumon @ Autohotkey forums, simon . stralberg @ gmail . com]
Autohotkey version: AHK_L (Unicode, x32)
CHANGELOG:
v.
- 0.81: Added URLStatus() check (returns error if URL is invalid/file does not exist)
- 0.8: Added HASHFile check, added cleanup
REQUIREMENTS:
- HashFile() by Deo, http://www.autohotkey.com/forum/viewtopic.php?t=71133
- URLStatus() is included
LICENSE: If no license documentation exists, [http]
Script created using Autohotkey [http]
*/
remoteResource(Resource, URL="", Directory="", TT="Display", MD5Sum="") ; by sumon
{
; First, a few aliases/standard values:
static BaseURL ; Remembered between calls. HOWEVER, note that the file type is not automatically appended
if ((Resource = "Load") or (Resource = "Init") or (Resource = "URL")) ; Used for cases where you want to set a base URL, f.ex. remoteResource("Load", "http://www.autohotkey.net/~sumon/img")
{
BaseURL := URL
return 1
}
If (!Directory)
{
SplitPath, Resource, , , Ext ; Extension
If (RegExMatch(Ext, "doc|docx|html|odt|rtf|txt|pdf|html|htm"))
Type := "Docs"
else if (RegExMatch(Ext, "ani|bmp|cur|gif|ico|jpg|jpeg|jp2|png|raw|tga|tif|tiff"))
Type := "Media\Images"
else if (RegExMatch(Ext, "aiff|raw|wav|flac|wma|mp3|aac|swa|mid"))
Type := "Media\Sounds"
else if (RegExMatch(Ext, "ini"))
Type := "Data"
else
Type := "Other"
SplitPath, A_ScriptName,,,, ScriptName ; Removes extension
Directory := A_AppData . "\" ScriptName . "\" . Type ; Store the data systematically
If ((URL = "GetDir") or (URL = "Dir"))
return A_AppData . "\" ScriptName ; A simple way to only retrieve the default directory
}
if (!InStr(URL, "." Ext))
URL := BaseURL . Resource
;
If (MD5Sum)
{
If (HashFile(Directory . "\" . Resource) = MD5Sum) ; Check MD5 integrity of file
return Directory . "\" . Resource
}
else if FileExist(Directory . "\" . Resource)
return Directory . "\" . Resource ; Exists, return path
else
{
If (URL)
{
If (!(URLStatus(URL) >= 200 AND URLStatus(URL) < 300)) ; If cannot connect
{
If (TT = "Display")
Traytip,, Error: %URLStatus%... ,, 1
Return URLStatus ; Error #, mostly 404
}
If (TT = "Display")
Traytip,, Downloading %Resource%... ,, 1 ; Optional
else if (TT)
Traytip,, %TT%,, 1
FileCreateDir, %Directory%
UrlDownloadToFile, %URL%, %Directory%\%Resource%
If (TT)
TrayTip ; Clear message
If (MD5Sum)
{
If (HashFile(Directory . "\" . Resource) != MD5Sum)
{
MsgBox, 48, Invalid resource, Warning! The file version (MD5) of %Resource% does not seem valid.
return ; Return nothing
}
}
return Directory . "\" . Resource ; File was downloaded
}
return 0 ; Couldn't download either
}
} ; Returns: 0 if could not download, 2 if HashSum is wrong on both local & online version, 404 (or error status) if file does not exist
remoteResource_Clear(Directory="", Resource="")
{
If (Directory AND (Directory != A_Temp) AND (!Resource)) ; If directory is not A_Temp or AppData\ScriptName, confirm
{
MsgBox, 308, WARNING!, %Directory% and all its' files is about to deleted! Are you sure?
IfMsgBox, No
return 0
}
SplitPath, A_ScriptName,,,, ScriptName ; Removes extension
Directory := (Directory)?(Directory):(A_AppData "\" ScriptName)
Resource := (Resource)?(Resource):("All")
If (Resource = "All")
{
MsgBox Removing %Directory%
FileRemoveDir, %Directory%, 1
}
else
{
fDir := remoteResource(Resource, "Dir") ; Retrieves the default dir (AppData\ScriptName\MediaType\res.ext)
return fDir
If FileExist(fDir)
FileDelete, %fDir%
}
}
URLStatus(URL="") ; By PEG & gamax92, http://www.autohotkey.com/forum/viewtopic.php?p=503855#503855
{
http:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
http.open("GET", url, false)s
http.send()
return http.Status
}Sample:
; Without rRes (example)
path := "data\img\"
If (!FileExist(path . "appifyerFrame.png"))
{
FileCreateDir, %path%
Traytip,, Downloading appifyerFrame.png... ,, 1
UrlDownloadToFile, http://autohotkey.net/~sumon/img/appifyerFrame.png, %path%\appifyerFrame.png
}
aFrame := path . "\appifyerFrame.png"
; With rRes
aFrame := remoteResource("appifyerFrame.png", "http://autohotkey.net/~sumon/img/appifyerFrame.png")
Demo (copy-n-paste):
;~ #Include <remoteResource> ; Uncomment if you need to
; # Example 1. Image
TrayTip, Example 1, Download an image`, then add it to a GUI,, 1
KeyWait, Enter, Up
pic := remoteResource("appifyerFrame.png", "http://www.autohotkey.net/~sumon/img/appifyerFrame.png")
Gui, Add, Pic,, %pic%
Gui, Add, Text,, Press Enter to continue (to example 1b)
Gui, Show,, Example 1
; # Example 1b. Use the image again
KeyWait, Enter, D T10
Gui, Destroy
Traytip, Example 1b, Note that the image can now be directly accessed without downloading it,, 1
Gui, Add, Pic, x10 w50 h30, %pic%
Gui, Add, Pic, xp+60 yp w50 h30, %pic%
Gui, Add, Pic, x10 yp+40 w100 h60, %pic%
Gui, Add, Text,, Press Enter to continue (to example 2)
Gui, Show,, Example 1b
KeyWait, Enter, D T10
; # Example 2. Download a sound file.
Gui, Destroy
Traytip, Example 2, Any file can be downloaded this way (f.ex. a sound file),, 1
music := remoteResource("taurus_sample.mp3", "http://www.autohotkey.net/~sumon/media/sounds/taurus_sample.mp3") ; This is from my IRL buddy DJ Taurus/Astreim, http://www.youtube.com/user/Astreim
Run %music%
Sleep 5000
TrayTip, Storage, Press Enter to see where the files are stored
KeyWait, Enter, D T15
DefaultDir := remoteResource("getDir")
Run %DefaultDir%
TrayTip, Finished, Demonstration is finished!
return
Esc:: ExitApp
/*
Function included in demonstration, for your convenience
*/
remoteResource(Resource, URL="", Directory="", TT="Display") ; by sumon
{
; First, a few aliases/standard values:
If (!Directory)
{
SplitPath, Resource, , , Ext ; Extension
If (RegExMatch(Ext, "doc|docx|html|odt|rtf|txt|pdf|html|htm"))
Type := "Docs"
else if (RegExMatch(Ext, "ani|bmp|cur|gif|ico|jpg|jpeg|jp2|png|raw|tga|tif|tiff"))
Type := "Media\Images"
else if (RegExMatch(Ext, "aiff|raw|wav|flac|wma|mp3|aac|swa|mid"))
Type := "Media\Sounds"
else if (RegExMatch(Ext, "ini"))
Type := "Data"
else
Type := "Other"
SplitPath, A_ScriptName,,,, ScriptName ; Removes extension
Directory := A_AppData . "\" ScriptName . "\" . Type ; Store the data systematically
If (Resource = "GetDir")
return A_AppData . "\" ScriptName ; A simple way to only retrieve the default directory
}
;
If FileExist(Directory . "\" . Resource)
return Directory . "\" . Resource ; Exists, return path
If (!FileExist(Directory . "\" . Resource))
{
If (URL)
{
If (TT = "Display")
Traytip,, Downloading %Resource%... ,, 1 ; Optional
else if (TT)
Traytip,, %TT%,, 1
FileCreateDir, %Directory%
UrlDownloadToFile, %URL%, %Directory%\%Resource%
If (TT)
TrayTip
return Directory . "\" . Resource ; File was downloaded
}
return 0 ; Couldn't download either
}
}




