diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2016-04-10 09:28:39 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2016-04-10 09:28:39 +0000 |
commit | 32761a6cee1d0dee366b885b7b9c777e67885688 (patch) | |
tree | d6bec92bebfb216f4126356e55518842c2f476a1 /Source/WebCore/fileapi/File.cpp | |
parent | a4e969f4965059196ca948db781e52f7cfebf19e (diff) | |
download | WebKitGtk-tarball-32761a6cee1d0dee366b885b7b9c777e67885688.tar.gz |
webkitgtk-2.4.11webkitgtk-2.4.11
Diffstat (limited to 'Source/WebCore/fileapi/File.cpp')
-rw-r--r-- | Source/WebCore/fileapi/File.cpp | 104 |
1 files changed, 72 insertions, 32 deletions
diff --git a/Source/WebCore/fileapi/File.cpp b/Source/WebCore/fileapi/File.cpp index 94b228823..6f6dd0e61 100644 --- a/Source/WebCore/fileapi/File.cpp +++ b/Source/WebCore/fileapi/File.cpp @@ -26,39 +26,77 @@ #include "config.h" #include "File.h" -#include "BlobURL.h" #include "FileMetadata.h" #include "FileSystem.h" #include "MIMETypeRegistry.h" -#include "ThreadableBlobRegistry.h" #include <wtf/CurrentTime.h> #include <wtf/DateMath.h> #include <wtf/text/WTFString.h> namespace WebCore { -File::File(const String& path) - : Blob(uninitializedContructor) +static String getContentTypeFromFileName(const String& name, File::ContentTypeLookupPolicy policy) +{ + String type; + int index = name.reverseFind('.'); + if (index != -1) { + if (policy == File::WellKnownContentTypes) + type = MIMETypeRegistry::getWellKnownMIMETypeForExtension(name.substring(index + 1)); + else { + ASSERT(policy == File::AllContentTypes); + type = MIMETypeRegistry::getMIMETypeForExtension(name.substring(index + 1)); + } + } + return type; +} + +static std::unique_ptr<BlobData> createBlobDataForFileWithType(const String& path, const String& contentType) +{ + auto blobData = std::make_unique<BlobData>(); + ASSERT(Blob::isNormalizedContentType(contentType)); + blobData->setContentType(contentType); + blobData->appendFile(path); + return blobData; +} + +static std::unique_ptr<BlobData> createBlobDataForFile(const String& path, File::ContentTypeLookupPolicy policy) +{ + return createBlobDataForFileWithType(path, getContentTypeFromFileName(path, policy)); +} + +static std::unique_ptr<BlobData> createBlobDataForFileWithName(const String& path, const String& fileSystemName, File::ContentTypeLookupPolicy policy) +{ + return createBlobDataForFileWithType(path, getContentTypeFromFileName(fileSystemName, policy)); +} + +#if ENABLE(DIRECTORY_UPLOAD) +PassRefPtr<File> File::createWithRelativePath(const String& path, const String& relativePath) +{ + RefPtr<File> file = adoptRef(new File(path, AllContentTypes)); + file->m_relativePath = relativePath; + return file.release(); +} +#endif + +File::File(const String& path, ContentTypeLookupPolicy policy) + : Blob(createBlobDataForFile(path, policy), -1) , m_path(path) + , m_name(pathGetFileName(path)) { - m_internalURL = BlobURL::createInternalURL(); - m_size = -1; - computeNameAndContentType(m_path, String(), m_name, m_type); - ThreadableBlobRegistry::registerFileBlobURL(m_internalURL, path, m_type); } -File::File(const String& path, const String& nameOverride) - : Blob(uninitializedContructor) +File::File(const String& path, const URL& url, const String& type) + : Blob(url, type, -1) , m_path(path) { - m_internalURL = BlobURL::createInternalURL(); - m_size = -1; - computeNameAndContentType(m_path, nameOverride, m_name, m_type); - ThreadableBlobRegistry::registerFileBlobURL(m_internalURL, path, m_type); + m_name = pathGetFileName(path); + // FIXME: File object serialization/deserialization does not include + // newer file object data members: m_name and m_relativePath. + // See SerializedScriptValue.cpp } -File::File(DeserializationContructor, const String& path, const URL& url, const String& type, const String& name) - : Blob(deserializationContructor, url, type, -1) +File::File(const String& path, const String& name, ContentTypeLookupPolicy policy) + : Blob(createBlobDataForFileWithName(path, name, policy), -1) , m_path(path) , m_name(name) { @@ -73,27 +111,29 @@ double File::lastModifiedDate() const return currentTime() * msPerSecond; } -void File::computeNameAndContentType(const String& path, const String& nameOverride, String& effectiveName, String& effectiveContentType) +unsigned long long File::size() const { -#if ENABLE(FILE_REPLACEMENT) - if (shouldReplaceFile(path)) { - computeNameAndContentTypeForReplacedFile(path, nameOverride, effectiveName, effectiveContentType); - return; - } -#endif - effectiveName = nameOverride.isNull() ? pathGetFileName(path) : nameOverride; - size_t index = effectiveName.reverseFind('.'); - if (index != notFound) - effectiveContentType = MIMETypeRegistry::getMIMETypeForExtension(effectiveName.substring(index + 1)); + // FIXME: JavaScript cannot represent sizes as large as unsigned long long, we need to + // come up with an exception to throw if file size is not representable. + long long size; + if (!getFileSize(m_path, size)) + return 0; + return static_cast<unsigned long long>(size); } -String File::contentTypeForFile(const String& path) +void File::captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const { - String name; - String type; - computeNameAndContentType(path, String(), name, type); + // Obtains a snapshot of the file by capturing its current size and modification time. This is used when we slice a file for the first time. + // If we fail to retrieve the size or modification time, probably due to that the file has been deleted, 0 size is returned. + FileMetadata metadata; + if (!getFileMetadata(m_path, metadata)) { + snapshotSize = 0; + snapshotModificationTime = invalidFileTime(); + return; + } - return type; + snapshotSize = metadata.length; + snapshotModificationTime = metadata.modificationTime; } } // namespace WebCore |