Skip to content

Truly independent web browser

2.4 KB
Raw Blame
/*
 * Copyright (c) 2023, Andrew Kaster <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/LexicalPath.h>
#include <LibCore/Resource.h>
#include <LibCore/ResourceImplementation.h>
#include <LibCore/System.h>

namespace Core {

Resource::Resource(String path, Scheme scheme, NonnullOwnPtr<Core::MappedFile> file, time_t modified_time)
    : m_path(move(path))
    , m_scheme(scheme)
    , m_data(move(file))
    , m_modified_time(modified_time)
{
}

Resource::Resource(String path, Scheme scheme, DirectoryTag, time_t modified_time)
    : m_path(move(path))
    , m_scheme(scheme)
    , m_data(DirectoryTag {})
    , m_modified_time(modified_time)
{
}

ErrorOr<NonnullRefPtr<Resource>> Resource::load_from_uri(StringView uri)
{
    return ResourceImplementation::the().load_from_uri(uri);
}

String Resource::uri() const
{
    return MUST(String::formatted("{}://{}", m_scheme == Scheme::Resource ? "resource"sv : "file"sv, m_path));
}

String Resource::filesystem_path() const
{
    return ResourceImplementation::the().filesystem_path(*this);
}

String Resource::file_url() const
{
    if (m_scheme == Scheme::File)
        return uri();

    return MUST(String::formatted("file://{}", filesystem_path()));
}

Optional<time_t> Resource::modified_time() const
{
    return m_modified_time;
}

String Resource::filename() const
{
    return MUST(String::from_utf8(LexicalPath(m_path.bytes_as_string_view()).basename()));
}

Vector<String> Resource::children() const
{
    return ResourceImplementation::the().child_names(*this);
}

ByteBuffer Resource::clone_data() const
{
    return m_data.visit(
        [](OwnPtr<Core::MappedFile> const& file) {
            VERIFY(file);
            return MUST(ByteBuffer::copy(file->bytes()));
        },
        [](DirectoryTag) -> ByteBuffer { VERIFY_NOT_REACHED(); });
}

ReadonlyBytes Resource::data() const
{
    return m_data.visit(
        [](OwnPtr<Core::MappedFile> const& file) {
            VERIFY(file);
            return file->bytes();
        },
        [](DirectoryTag) -> ReadonlyBytes { VERIFY_NOT_REACHED(); });
}

NonnullOwnPtr<Core::MappedFile> Resource::release_mapped_file()
{
    return m_data.visit(
        [](OwnPtr<Core::MappedFile>& file) { return file.release_nonnull(); },
        [](DirectoryTag) -> NonnullOwnPtr<Core::MappedFile> { VERIFY_NOT_REACHED(); });
}

FixedMemoryStream Resource::stream() const
{
    return FixedMemoryStream(data());
}

}