#include "config.h"
#include "File.h"
#include "FileSystem.h"
#include "MIMETypeRegistry.h"
#include <wtf/CurrentTime.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
static PassOwnPtr<BlobData> createBlobDataForFileWithType(const String& path, const String& contentType)
{
OwnPtr<BlobData> blobData = BlobData::create();
blobData->setContentType(contentType);
blobData->appendFile(path);
return blobData.release();
}
static PassOwnPtr<BlobData> createBlobDataForFile(const String& path)
{
String type;
int index = path.reverseFind('.');
if (index != -1)
type = MIMETypeRegistry::getMIMETypeForExtension(path.substring(index + 1));
return createBlobDataForFileWithType(path, type);
}
static PassOwnPtr<BlobData> createBlobDataForFileWithName(const String& path, const String& fileSystemName)
{
String type;
int index = fileSystemName.reverseFind('.');
if (index != -1)
type = MIMETypeRegistry::getWellKnownMIMETypeForExtension(fileSystemName.substring(index + 1));
return createBlobDataForFileWithType(path, type);
}
#if ENABLE(DIRECTORY_UPLOAD)
PassRefPtr<File> File::createWithRelativePath(const String& path, const String& relativePath)
{
RefPtr<File> file = adoptRef(new File(path));
file->m_relativePath = relativePath;
return file.release();
}
#endif
File::File(const String& path)
: Blob(createBlobDataForFile(path), -1)
, m_path(path)
, m_name(pathGetFileName(path))
{
}
File::File(const String& path, const KURL& url, const String& type)
: Blob(url, type, -1)
, m_path(path)
{
m_name = pathGetFileName(path);
}
File::File(const String& path, const String& name)
: Blob(createBlobDataForFileWithName(path, name), -1)
, m_path(path)
, m_name(name)
{
}
double File::lastModifiedDate() const
{
time_t modificationTime;
if (!getFileModificationTime(m_path, modificationTime))
return 0;
return modificationTime * 1000.0;
}
unsigned long long File::size() const
{
long long size;
if (!getFileSize(m_path, size))
return 0;
return static_cast<unsigned long long>(size);
}
void File::captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const
{
time_t modificationTime;
if (!getFileSize(m_path, snapshotSize) || !getFileModificationTime(m_path, modificationTime)) {
snapshotSize = 0;
snapshotModificationTime = 0;
} else
snapshotModificationTime = modificationTime;
}
}