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/text/WTFString.h>
37 static String getContentTypeFromFileName(const String& name, File::ContentTypeLookupPolicy policy)
40 int index = name.reverseFind('.');
42 if (policy == File::WellKnownContentTypes)
43 type = MIMETypeRegistry::getWellKnownMIMETypeForExtension(name.substring(index + 1));
45 ASSERT(policy == File::AllContentTypes);
46 type = MIMETypeRegistry::getMIMETypeForExtension(name.substring(index + 1));
52 static PassOwnPtr<BlobData> createBlobDataForFileWithType(const String& path, const String& contentType)
54 OwnPtr<BlobData> blobData = BlobData::create();
55 blobData->setContentType(contentType);
56 blobData->appendFile(path);
57 return blobData.release();
60 static PassOwnPtr<BlobData> createBlobDataForFile(const String& path, File::ContentTypeLookupPolicy policy)
62 return createBlobDataForFileWithType(path, getContentTypeFromFileName(path, policy));
65 static PassOwnPtr<BlobData> createBlobDataForFileWithName(const String& path, const String& fileSystemName, File::ContentTypeLookupPolicy policy)
67 return createBlobDataForFileWithType(path, getContentTypeFromFileName(fileSystemName, policy));
70 #if ENABLE(FILE_SYSTEM)
71 static PassOwnPtr<BlobData> createBlobDataForFileWithMetadata(const String& fileSystemName, const FileMetadata& metadata)
73 OwnPtr<BlobData> blobData = BlobData::create();
74 blobData->setContentType(getContentTypeFromFileName(fileSystemName, File::WellKnownContentTypes));
75 blobData->appendFile(metadata.platformPath, 0, metadata.length, metadata.modificationTime);
76 return blobData.release();
79 static PassOwnPtr<BlobData> createBlobDataForFileSystemURL(const KURL& fileSystemURL, const FileMetadata& metadata)
81 OwnPtr<BlobData> blobData = BlobData::create();
82 blobData->setContentType(getContentTypeFromFileName(fileSystemURL.path(), File::WellKnownContentTypes));
83 blobData->appendURL(fileSystemURL, 0, metadata.length, metadata.modificationTime);
84 return blobData.release();
88 #if ENABLE(DIRECTORY_UPLOAD)
89 PassRefPtr<File> File::createWithRelativePath(const String& path, const String& relativePath)
91 RefPtr<File> file = adoptRef(new File(path, AllContentTypes));
92 file->m_relativePath = relativePath;
93 return file.release();
97 File::File(const String& path, ContentTypeLookupPolicy policy)
98 : Blob(createBlobDataForFile(path, policy), -1)
100 , m_name(pathGetFileName(path))
101 #if ENABLE(FILE_SYSTEM)
103 , m_snapshotModificationTime(invalidFileTime())
108 File::File(const String& path, const KURL& url, const String& type)
109 : Blob(url, type, -1)
111 #if ENABLE(FILE_SYSTEM)
113 , m_snapshotModificationTime(invalidFileTime())
116 m_name = pathGetFileName(path);
117 // FIXME: File object serialization/deserialization does not include
118 // newer file object data members: m_name and m_relativePath.
119 // See SerializedScriptValue.cpp for js and v8.
122 File::File(const String& path, const String& name, ContentTypeLookupPolicy policy)
123 : Blob(createBlobDataForFileWithName(path, name, policy), -1)
126 #if ENABLE(FILE_SYSTEM)
128 , m_snapshotModificationTime(invalidFileTime())
133 #if ENABLE(FILE_SYSTEM)
134 File::File(const String& name, const FileMetadata& metadata)
135 : Blob(createBlobDataForFileWithMetadata(name, metadata), metadata.length)
136 , m_path(metadata.platformPath)
138 , m_snapshotSize(metadata.length)
139 , m_snapshotModificationTime(metadata.modificationTime)
143 File::File(const KURL& fileSystemURL, const FileMetadata& metadata)
144 : Blob(createBlobDataForFileSystemURL(fileSystemURL, metadata), metadata.length)
145 , m_fileSystemURL(fileSystemURL)
146 , m_snapshotSize(metadata.length)
147 , m_snapshotModificationTime(metadata.modificationTime)
152 double File::lastModifiedDate() const
154 #if ENABLE(FILE_SYSTEM)
155 if (hasValidSnapshotMetadata())
156 return m_snapshotModificationTime * 1000.0;
159 time_t modificationTime;
160 if (!getFileModificationTime(m_path, modificationTime))
161 return invalidFileTime();
163 // Needs to return epoch time in milliseconds for Date.
164 return modificationTime * 1000.0;
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