#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)
, m_path(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)
, 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);
}
File::File(DeserializationContructor, const String& path, const URL& url, const String& type, const String& name)
: Blob(deserializationContructor, url, type, -1, path)
, m_path(path)
, m_name(name)
{
}
static BlobPropertyBag convertPropertyBag(const File::PropertyBag& initialBag)
{
BlobPropertyBag bag;
bag.type = initialBag.type;
return bag;
}
File::File(Vector<BlobPartVariant>&& blobPartVariants, const String& filename, const PropertyBag& propertyBag)
: Blob(WTFMove(blobPartVariants), convertPropertyBag(propertyBag))
, m_name(filename)
, m_overrideLastModifiedDate(propertyBag.lastModified.value_or(currentTimeMS()))
{
}
double File::lastModified() const
{
if (m_overrideLastModifiedDate)
return m_overrideLastModifiedDate.value();
double result;
time_t modificationTime;
if (getFileModificationTime(m_path, modificationTime) && isValidFileTime(modificationTime))
result = modificationTime * msPerSecond;
else
result = currentTime() * msPerSecond;
return WTF::timeClip(result);
}
void File::computeNameAndContentType(const String& path, const String& nameOverride, String& effectiveName, String& effectiveContentType)
{
#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));
}
String File::contentTypeForFile(const String& path)
{
String name;
String type;
computeNameAndContentType(path, String(), name, type);
return type;
}
}