2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "FileMetadata.h"
30 #include "FileSystem.h"
31 #include "MIMETypeRegistry.h"
32 #include <wtf/CurrentTime.h>
33 #include <wtf/DateMath.h>
34 #include <wtf/text/WTFString.h>
38 static String getContentTypeFromFileName(const String& name, File::ContentTypeLookupPolicy policy)
41 int index = name.reverseFind('.');
43 if (policy == File::WellKnownContentTypes)
44 type = MIMETypeRegistry::getWellKnownMIMETypeForExtension(name.substring(index + 1));
46 ASSERT(policy == File::AllContentTypes);
47 type = MIMETypeRegistry::getMIMETypeForExtension(name.substring(index + 1));
53 static PassOwnPtr<BlobData> createBlobDataForFileWithType(const String& path, const String& contentType)
55 OwnPtr<BlobData> blobData = BlobData::create();
56 blobData->setContentType(contentType);
57 blobData->appendFile(path);
58 return blobData.release();
61 static PassOwnPtr<BlobData> createBlobDataForFile(const String& path, File::ContentTypeLookupPolicy policy)
63 return createBlobDataForFileWithType(path, getContentTypeFromFileName(path, policy));
66 static PassOwnPtr<BlobData> createBlobDataForFileWithName(const String& path, const String& fileSystemName, File::ContentTypeLookupPolicy policy)
68 return createBlobDataForFileWithType(path, getContentTypeFromFileName(fileSystemName, policy));
71 #if ENABLE(FILE_SYSTEM)
72 static PassOwnPtr<BlobData> createBlobDataForFileWithMetadata(const String& fileSystemName, const FileMetadata& metadata)
74 OwnPtr<BlobData> blobData = BlobData::create();
75 blobData->setContentType(getContentTypeFromFileName(fileSystemName, File::WellKnownContentTypes));
76 blobData->appendFile(metadata.platformPath, 0, metadata.length, metadata.modificationTime);
77 return blobData.release();
80 static PassOwnPtr<BlobData> createBlobDataForFileSystemURL(const KURL& fileSystemURL, const FileMetadata& metadata)
82 OwnPtr<BlobData> blobData = BlobData::create();
83 blobData->setContentType(getContentTypeFromFileName(fileSystemURL.path(), File::WellKnownContentTypes));
84 blobData->appendURL(fileSystemURL, 0, metadata.length, metadata.modificationTime);
85 return blobData.release();
89 #if ENABLE(DIRECTORY_UPLOAD)
90 PassRefPtr<File> File::createWithRelativePath(const String& path, const String& relativePath)
92 RefPtr<File> file = adoptRef(new File(path, AllContentTypes));
93 file->m_relativePath = relativePath;
94 return file.release();
98 File::File(const String& path, ContentTypeLookupPolicy policy)
99 : Blob(createBlobDataForFile(path, policy), -1)
101 , m_name(pathGetFileName(path))
102 #if ENABLE(FILE_SYSTEM)
104 , m_snapshotModificationTime(invalidFileTime())
109 File::File(const String& path, const KURL& url, const String& type)
110 : Blob(url, type, -1)
112 #if ENABLE(FILE_SYSTEM)
114 , m_snapshotModificationTime(invalidFileTime())
117 m_name = pathGetFileName(path);
118 // FIXME: File object serialization/deserialization does not include
119 // newer file object data members: m_name and m_relativePath.
120 // See SerializedScriptValue.cpp for js and v8.
123 File::File(const String& path, const String& name, ContentTypeLookupPolicy policy)
124 : Blob(createBlobDataForFileWithName(path, name, policy), -1)
127 #if ENABLE(FILE_SYSTEM)
129 , m_snapshotModificationTime(invalidFileTime())
134 #if ENABLE(FILE_SYSTEM)
135 File::File(const String& name, const FileMetadata& metadata)
136 : Blob(createBlobDataForFileWithMetadata(name, metadata), metadata.length)
137 , m_path(metadata.platformPath)
139 , m_snapshotSize(metadata.length)
140 , m_snapshotModificationTime(metadata.modificationTime)
144 File::File(const KURL& fileSystemURL, const FileMetadata& metadata)
145 : Blob(createBlobDataForFileSystemURL(fileSystemURL, metadata), metadata.length)
146 , m_fileSystemURL(fileSystemURL)
147 , m_snapshotSize(metadata.length)
148 , m_snapshotModificationTime(metadata.modificationTime)
153 double File::lastModifiedDate() const
155 #if ENABLE(FILE_SYSTEM)
156 if (hasValidSnapshotMetadata() && isValidFileTime(m_snapshotModificationTime))
157 return m_snapshotModificationTime * msPerSecond;
160 time_t modificationTime;
161 if (getFileModificationTime(m_path, modificationTime) && isValidFileTime(modificationTime))
162 return modificationTime * msPerSecond;
164 return currentTime() * msPerSecond;
167 unsigned long long File::size() const
169 #if ENABLE(FILE_SYSTEM)
170 if (hasValidSnapshotMetadata())
171 return m_snapshotSize;
174 // FIXME: JavaScript cannot represent sizes as large as unsigned long long, we need to
175 // come up with an exception to throw if file size is not representable.
177 if (!getFileSize(m_path, size))
179 return static_cast<unsigned long long>(size);
182 void File::captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const
184 #if ENABLE(FILE_SYSTEM)
185 if (hasValidSnapshotMetadata()) {
186 snapshotSize = m_snapshotSize;
187 snapshotModificationTime = m_snapshotModificationTime;
192 // 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.
193 // If we fail to retrieve the size or modification time, probably due to that the file has been deleted, 0 size is returned.
194 FileMetadata metadata;
195 if (!getFileMetadata(m_path, metadata)) {
197 snapshotModificationTime = invalidFileTime();
201 snapshotSize = metadata.length;
202 snapshotModificationTime = metadata.modificationTime;
205 } // namespace WebCore