Bug 814376 - Improve the mechanism for setting file extension of a blob, r=sicking

This commit is contained in:
Eric Chou 2012-11-26 20:51:29 +08:00
Родитель 57cf75c47a
Коммит 3c21e29226
1 изменённых файлов: 37 добавлений и 10 удалений

Просмотреть файл

@ -17,13 +17,14 @@
#include "mozilla/RefPtr.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPtr.h"
#include "nsCExternalHandlerService.h"
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "nsIDOMFile.h"
#include "nsIFile.h"
#include "nsIInputStream.h"
#include "nsIMIMEService.h"
#include "nsIOutputStream.h"
#include "nsLocalFile.h"
#include "nsNetUtil.h"
#define TARGET_FOLDER "/sdcard/downloads/bluetooth/"
@ -283,22 +284,48 @@ BluetoothOppManager::SendFile(BlobParent* aActor)
*/
mBlob = aActor->GetBlob();
nsCOMPtr<nsIDOMFile> domFile = do_QueryInterface(mBlob);
nsString fullPath;
sFileName.Truncate();
if (domFile && NS_SUCCEEDED(domFile->GetMozFullPathInternal(fullPath))) {
nsCOMPtr<nsIFile> localFile = new nsLocalFile();
NS_NewLocalFile(fullPath, false, getter_AddRefs(localFile));
if (localFile) {
localFile->GetLeafName(sFileName);
}
nsCOMPtr<nsIDOMFile> file = do_QueryInterface(mBlob);
if (file) {
file->GetName(sFileName);
}
/**
* We try our best to get the file extention to avoid interoperability issues.
* However, once we found that we are unable to get suitable extension or
* information about the content type, sending a pre-defined file name without
* extension would be fine.
*/
if (sFileName.IsEmpty()) {
sFileName.AssignLiteral("Unknown");
}
int32_t offset = sFileName.RFindChar('/');
if (offset != kNotFound) {
sFileName = Substring(sFileName, offset + 1);
}
offset = sFileName.RFindChar('.');
if (offset == kNotFound) {
nsCOMPtr<nsIMIMEService> mimeSvc = do_GetService(NS_MIMESERVICE_CONTRACTID);
if (mimeSvc) {
nsString mimeType;
mBlob->GetType(mimeType);
nsCString extension;
nsresult rv =
mimeSvc->GetPrimaryExtension(NS_LossyConvertUTF16toASCII(mimeType),
EmptyCString(),
extension);
if (NS_SUCCEEDED(rv)) {
sFileName.AppendLiteral(".");
AppendUTF8toUTF16(extension, sFileName);
}
}
}
SendConnectRequest();
return true;