Class: Mbeditor::FileImportService

Inherits:
Object
  • Object
show all
Defined in:
app/services/mbeditor/file_import_service.rb

Overview

Writes files dragged into the explorer from outside the browser.

Conflict handling is a two-pass protocol driven by the client. Pass one runs with on_conflict: :ask — every entry whose target is free is written, and the rest come back under :conflicts untouched. The client resolves them and re-sends just those entries with :overwrite or :rename. Because the existence check and the write happen inside the same call, the window is closed at the protocol level: the client never gets to check in one round trip and write in another. (File.exist? and File.open are still two syscalls, so this is not an atomic guarantee against other processes.)

Callers must hand over target paths that have already cleared the controller's resolve_path / path_blocked_for_operations? guards; this service does no sandboxing of its own.

Constant Summary collapse

MAX_FILE_SIZE_BYTES =
FileOperationService::MAX_FILE_SIZE_BYTES
CONFLICT_MODES =
%i[ask overwrite rename].freeze

Instance Method Summary collapse

Constructor Details

#initialize(workspace_root) ⇒ FileImportService

Returns a new instance of FileImportService.



25
26
27
# File 'app/services/mbeditor/file_import_service.rb', line 25

def initialize(workspace_root)
  @workspace_root = Pathname(workspace_root)
end

Instance Method Details

#import(entries, on_conflict: :ask) ⇒ Object

entries: [{ target_path: , io: <#read, #rewind, #size> }] => { imported: [name:], conflicts: [path:], errors: [error:] }

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
# File 'app/services/mbeditor/file_import_service.rb', line 31

def import(entries, on_conflict: :ask)
  mode = on_conflict.to_s.to_sym
  raise ArgumentError, "unknown on_conflict: #{on_conflict.inspect}" unless CONFLICT_MODES.include?(mode)

  result = { imported: [], conflicts: [], errors: [] }
  Array(entries).each { |entry| import_entry(entry, mode, result) }
  result
end