diff --git a/Userland/Applications/PixelPaint/Image.cpp b/Userland/Applications/PixelPaint/Image.cpp index 366171a4116..71b749a15b8 100644 --- a/Userland/Applications/PixelPaint/Image.cpp +++ b/Userland/Applications/PixelPaint/Image.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -140,9 +141,8 @@ RefPtr Image::try_create_from_file(String const& file_path) return Image::try_create_from_bitmap(bitmap.release_nonnull()); } -void Image::save(String const& file_path) const +Result Image::write_to_file(const String& file_path) const { - // Build json file StringBuilder builder; JsonObjectSerializer json(builder); json.add("width", m_size.width()); @@ -165,11 +165,13 @@ void Image::save(String const& file_path) const } json.finish(); - // Write json to disk - auto file = fopen(file_path.characters(), "w"); - auto byte_buffer = builder.to_byte_buffer(); - fwrite(byte_buffer.data(), sizeof(u8), byte_buffer.size(), file); - fclose(file); + auto file_or_error = Core::File::open(file_path, (Core::OpenMode)(Core::OpenMode::WriteOnly | Core::OpenMode::Truncate)); + if (file_or_error.is_error()) + return file_or_error.error(); + + if (!file_or_error.value()->write(builder.string_view())) + return String { file_or_error.value()->error_string() }; + return {}; } void Image::export_bmp(String const& file_path) diff --git a/Userland/Applications/PixelPaint/Image.h b/Userland/Applications/PixelPaint/Image.h index cb5090d94b9..cd109e182bc 100644 --- a/Userland/Applications/PixelPaint/Image.h +++ b/Userland/Applications/PixelPaint/Image.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -52,7 +53,7 @@ public: void restore_snapshot(Image const&); void paint_into(GUI::Painter&, Gfx::IntRect const& dest_rect); - void save(String const& file_path) const; + Result write_to_file(String const& file_path) const; void export_bmp(String const& file_path); void export_png(String const& file_path); diff --git a/Userland/Applications/PixelPaint/main.cpp b/Userland/Applications/PixelPaint/main.cpp index 8c17530c22c..89634fcdeef 100644 --- a/Userland/Applications/PixelPaint/main.cpp +++ b/Userland/Applications/PixelPaint/main.cpp @@ -115,10 +115,14 @@ int main(int argc, char** argv) auto save_image_as_action = GUI::CommonActions::make_save_as_action([&](auto&) { if (!image_editor.image()) return; - Optional save_path = GUI::FilePicker::get_save_filepath(window, "untitled", "pp"); + auto save_path = GUI::FilePicker::get_save_filepath(window, "untitled", "pp"); if (!save_path.has_value()) return; - image_editor.image()->save(save_path.value()); + auto result = image_editor.image()->write_to_file(save_path.value()); + if (result.is_error()) { + GUI::MessageBox::show_error(window, String::formatted("Could not save {}: {}", save_path.value(), result.error())); + return; + } }); auto menubar = GUI::Menubar::construct();