Class: Mbeditor::FileOperationService
- Inherits:
-
Object
- Object
- Mbeditor::FileOperationService
show all
- Defined in:
- app/services/mbeditor/file_operation_service.rb
Defined Under Namespace
Classes: FileExistsError, FileTooLargeError, PathNotFoundError, TargetExistsError
Constant Summary
collapse
- MAX_FILE_SIZE_BYTES =
5 * 1024 * 1024
Instance Method Summary
collapse
Constructor Details
Returns a new instance of FileOperationService.
14
15
16
|
# File 'app/services/mbeditor/file_operation_service.rb', line 14
def initialize(workspace_root)
@workspace_root = Pathname(workspace_root)
end
|
Instance Method Details
#create_dir(path) ⇒ Object
46
47
48
49
50
51
|
# File 'app/services/mbeditor/file_operation_service.rb', line 46
def create_dir(path)
raise FileExistsError if File.exist?(path)
FileUtils.mkdir_p(path)
{ ok: true, type: "folder", path: relative_path(path), name: File.basename(path) }
end
|
#create_file(path, content) ⇒ Object
53
54
55
56
57
58
59
60
|
# File 'app/services/mbeditor/file_operation_service.rb', line 53
def create_file(path, content)
raise FileTooLargeError if content.bytesize > MAX_FILE_SIZE_BYTES
raise FileExistsError if File.exist?(path)
FileUtils.mkdir_p(File.dirname(path))
File.write(path, content)
{ ok: true, type: "file", path: relative_path(path), name: File.basename(path) }
end
|
#destroy_path(path) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
|
# File 'app/services/mbeditor/file_operation_service.rb', line 25
def destroy_path(path)
return { ok: true } unless File.exist?(path)
if File.directory?(path)
FileUtils.rm_rf(path)
{ ok: true, type: "folder", path: relative_path(path) }
else
File.delete(path)
{ ok: true, type: "file", path: relative_path(path) }
end
end
|
#rename(old_path, new_path) ⇒ Object
37
38
39
40
41
42
43
44
|
# File 'app/services/mbeditor/file_operation_service.rb', line 37
def rename(old_path, new_path)
raise PathNotFoundError unless File.exist?(old_path)
raise TargetExistsError if File.exist?(new_path)
FileUtils.mkdir_p(File.dirname(new_path))
FileUtils.mv(old_path, new_path)
{ ok: true, oldPath: relative_path(old_path), path: relative_path(new_path), name: File.basename(new_path) }
end
|
#save(path, content) ⇒ Object
18
19
20
21
22
23
|
# File 'app/services/mbeditor/file_operation_service.rb', line 18
def save(path, content)
raise FileTooLargeError if content.bytesize > MAX_FILE_SIZE_BYTES
File.write(path, content)
{ ok: true, path: relative_path(path) }
end
|