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 "FileSystem.h"
30 #include "MIMETypeRegistry.h"
31 #include <wtf/CurrentTime.h>
32 #include <wtf/text/WTFString.h>
36 static PassOwnPtr<BlobData> createBlobDataForFileWithType(const String& path, const String& contentType)
38 OwnPtr<BlobData> blobData = BlobData::create();
39 blobData->setContentType(contentType);
40 blobData->appendFile(path);
41 return blobData.release();
44 static PassOwnPtr<BlobData> createBlobDataForFile(const String& path)
47 int index = path.reverseFind('.');
49 type = MIMETypeRegistry::getMIMETypeForExtension(path.substring(index + 1));
50 return createBlobDataForFileWithType(path, type);
53 #if ENABLE(FILE_SYSTEM)
54 static PassOwnPtr<BlobData> createBlobDataForFileSystemFile(const String& path, const String& fileSystemName)
57 int index = fileSystemName.reverseFind('.');
59 type = MIMETypeRegistry::getWellKnownMIMETypeForExtension(fileSystemName.substring(index + 1));
60 return createBlobDataForFileWithType(path, type);
64 #if ENABLE(DIRECTORY_UPLOAD)
65 PassRefPtr<File> File::createWithRelativePath(const String& path, const String& relativePath)
67 RefPtr<File> file = adoptRef(new File(path));
68 file->m_relativePath = relativePath;
69 return file.release();
73 File::File(const String& path)
74 : Blob(createBlobDataForFile(path), -1)
76 , m_name(pathGetFileName(path))
80 File::File(const String& path, const KURL& url, const String& type)
84 m_name = pathGetFileName(path);
85 // FIXME: File object serialization/deserialization does not include
86 // newer file object data members: m_name and m_relativePath.
87 // See SerializedScriptValue.cpp for js and v8.
90 #if ENABLE(FILE_SYSTEM)
91 File::File(const String& path, const String& name)
92 : Blob(createBlobDataForFileSystemFile(path, name), -1)
99 double File::lastModifiedDate() const
101 time_t modificationTime;
102 if (!getFileModificationTime(m_path, modificationTime))
105 // Needs to return epoch time in milliseconds for Date.
106 return modificationTime * 1000.0;
109 unsigned long long File::size() const
111 // FIXME: JavaScript cannot represent sizes as large as unsigned long long, we need to
112 // come up with an exception to throw if file size is not representable.
114 if (!getFileSize(m_path, size))
116 return static_cast<unsigned long long>(size);
119 void File::captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const
121 // 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.
122 // If we fail to retrieve the size or modification time, probably due to that the file has been deleted, 0 size is returned.
123 // FIXME: Combine getFileSize and getFileModificationTime into one file system call.
124 time_t modificationTime;
125 if (!getFileSize(m_path, snapshotSize) || !getFileModificationTime(m_path, modificationTime)) {
127 snapshotModificationTime = 0;
129 snapshotModificationTime = modificationTime;
132 } // namespace WebCore