Beispiel #1
0
nsresult
net_GetURLSpecFromActualFile(nsIFile *aFile, nsACString &result)
{
    nsresult rv;
    nsAutoString path;
  
    // construct URL spec from file path
    rv = aFile->GetPath(path);
    if (NS_FAILED(rv)) return rv;
  
    // Replace \ with / to convert to an url
    path.ReplaceChar(char16_t(0x5Cu), char16_t(0x2Fu));

    nsAutoCString escPath;

    // Windows Desktop paths begin with a drive letter, so need an 'extra'
    // slash at the begining
    // C:\Windows =>  file:///C:/Windows
    NS_NAMED_LITERAL_CSTRING(prefix, "file:///");

    // Escape the path with the directory mask
    NS_ConvertUTF16toUTF8 ePath(path);
    if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath))
        escPath.Insert(prefix, 0);
    else
        escPath.Assign(prefix + ePath);

    // esc_Directory does not escape the semicolons, so if a filename 
    // contains semicolons we need to manually escape them.
    // This replacement should be removed in bug #473280
    escPath.ReplaceSubstring(";", "%3b");

    result = escPath;
    return NS_OK;
}
nsresult
net_GetURLSpecFromFile(nsIFile *aFile, nsACString &result)
{
    nsresult rv;
    nsAutoString path;
  
    // construct URL spec from file path
    rv = aFile->GetPath(path);
    if (NS_FAILED(rv)) return rv;
  
    // Replace \ with / to convert to an url
    path.ReplaceChar(PRUnichar(0x5Cu), PRUnichar(0x2Fu));

    nsCAutoString escPath;
    NS_NAMED_LITERAL_CSTRING(prefix, "file:///");
  
    // Escape the path with the directory mask
    NS_ConvertUTF16toUTF8 ePath(path);
    if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath))
        escPath.Insert(prefix, 0);
    else
        escPath.Assign(prefix + ePath);

    // esc_Directory does not escape the semicolons, so if a filename 
    // contains semicolons we need to manually escape them.
    escPath.ReplaceSubstring(";", "%3b");

    // if this file references a directory, then we need to ensure that the
    // URL ends with a slash.  this is important since it affects the rules
    // for relative URL resolution when this URL is used as a base URL.
    // if the file does not exist, then we make no assumption about its type,
    // and simply leave the URL unmodified.
    if (escPath.Last() != '/') {
        PRBool dir;
        rv = aFile->IsDirectory(&dir);
        if (NS_SUCCEEDED(rv) && dir)
            escPath += '/';
    }
    
    result = escPath;
    return NS_OK;
}