Class: Rufio::FileOperations

Inherits:
Object
  • Object
show all
Defined in:
lib/rufio/file_operations.rb

Overview

Handles file operations (move, copy, delete)

Defined Under Namespace

Classes: OperationResult

Instance Method Summary collapse

Instance Method Details

#copy(items, source_directory, destination) ⇒ OperationResult

Copy files/directories to destination

Parameters:

  • items (Array<String>)

    Item names to copy

  • source_directory (String)

    Source directory path

  • destination (String)

    Destination directory path

Returns:



26
27
28
# File 'lib/rufio/file_operations.rb', line 26

def copy(items, source_directory, destination)
  perform_operation(:copy, items, source_directory, destination)
end

#create_directory(parent_directory, dirname) ⇒ OperationResult

Create a new directory

Parameters:

  • parent_directory (String)

    Parent directory path

  • dirname (String)

    Directory name

Returns:



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rufio/file_operations.rb', line 191

def create_directory(parent_directory, dirname)
  # Validate dirname
  if dirname.include?('/') || dirname.include?('\\')
    return OperationResult.new(
      success: false,
      message: 'Invalid directory name: cannot contain path separators',
      count: 0,
      errors: []
    )
  end

  dir_path = File.join(parent_directory, dirname)

  # Check if directory already exists
  if File.exist?(dir_path)
    return OperationResult.new(
      success: false,
      message: 'Directory already exists',
      count: 0,
      errors: []
    )
  end

  begin
    Dir.mkdir(dir_path)
    OperationResult.new(
      success: true,
      message: "Directory created: #{dirname}",
      count: 1,
      errors: []
    )
  rescue StandardError => e
    OperationResult.new(
      success: false,
      message: "Creation error: #{e.message}",
      count: 0,
      errors: [e.message]
    )
  end
end

#create_file(directory, filename) ⇒ OperationResult

Create a new file

Parameters:

  • directory (String)

    Directory path

  • filename (String)

    File name

Returns:



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rufio/file_operations.rb', line 146

def create_file(directory, filename)
  # Validate filename
  if filename.include?('/') || filename.include?('\\')
    return OperationResult.new(
      success: false,
      message: 'Invalid filename: cannot contain path separators',
      count: 0,
      errors: []
    )
  end

  file_path = File.join(directory, filename)

  # Check if file already exists
  if File.exist?(file_path)
    return OperationResult.new(
      success: false,
      message: 'File already exists',
      count: 0,
      errors: []
    )
  end

  begin
    File.write(file_path, '')
    OperationResult.new(
      success: true,
      message: "File created: #{filename}",
      count: 1,
      errors: []
    )
  rescue StandardError => e
    OperationResult.new(
      success: false,
      message: "Creation error: #{e.message}",
      count: 0,
      errors: [e.message]
    )
  end
end

#delete(items, source_directory) ⇒ OperationResult

Delete files/directories

Parameters:

  • items (Array<String>)

    Item names to delete

  • source_directory (String)

    Source directory path

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rufio/file_operations.rb', line 34

def delete(items, source_directory)
  success_count = 0
  error_messages = []

  items.each do |item_name|
    item_path = File.join(source_directory, item_name)

    begin
      # Check if file/directory exists
      unless File.exist?(item_path)
        error_messages << "#{item_name}: File not found"
        next
      end

      is_directory = File.directory?(item_path)

      if is_directory
        FileUtils.rm_rf(item_path)
      else
        FileUtils.rm(item_path)
      end

      # Verify deletion
      sleep(0.01) # Wait for file system sync
      if File.exist?(item_path)
        error_messages << "#{item_name}: Deletion failed"
      else
        success_count += 1
      end
    rescue StandardError => e
      error_messages << "#{item_name}: #{e.message}"
    end
  end

  OperationResult.new(
    success: error_messages.empty?,
    message: build_delete_message(success_count, items.length),
    count: success_count,
    errors: error_messages
  )
end

#move(items, source_directory, destination) ⇒ OperationResult

Move files/directories to destination

Parameters:

  • items (Array<String>)

    Item names to move

  • source_directory (String)

    Source directory path

  • destination (String)

    Destination directory path

Returns:



17
18
19
# File 'lib/rufio/file_operations.rb', line 17

def move(items, source_directory, destination)
  perform_operation(:move, items, source_directory, destination)
end

#rename(directory, old_name, new_name) ⇒ OperationResult

Rename a file or directory

Parameters:

  • directory (String)

    Directory path

  • old_name (String)

    Current name

  • new_name (String)

    New name

Returns:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rufio/file_operations.rb', line 81

def rename(directory, old_name, new_name)
  # Validate new_name
  if new_name.include?('/') || new_name.include?('\\')
    return OperationResult.new(
      success: false,
      message: 'Invalid name: cannot contain path separators',
      count: 0,
      errors: []
    )
  end

  if new_name.strip.empty?
    return OperationResult.new(
      success: false,
      message: 'Invalid name: cannot be empty',
      count: 0,
      errors: []
    )
  end

  old_path = File.join(directory, old_name)
  new_path = File.join(directory, new_name)

  # Check if old file exists
  unless File.exist?(old_path)
    return OperationResult.new(
      success: false,
      message: 'File not found',
      count: 0,
      errors: []
    )
  end

  # Check if new name already exists
  if File.exist?(new_path)
    return OperationResult.new(
      success: false,
      message: 'File already exists with that name',
      count: 0,
      errors: []
    )
  end

  begin
    FileUtils.mv(old_path, new_path)
    OperationResult.new(
      success: true,
      message: "Renamed: #{old_name}#{new_name}",
      count: 1,
      errors: []
    )
  rescue StandardError => e
    OperationResult.new(
      success: false,
      message: "Rename error: #{e.message}",
      count: 0,
      errors: [e.message]
    )
  end
end