class WarpEngine::FileManagerService
File browser of the file container (WarpEngine.config.file_container_path) for the admin: lists, uploads, renames, deletes and creates entries. Every path is relative to the container and confined to it; a path or name that would leave it raises ArgumentError.
Public Instance Methods
Source
# File app/services/warp_engine/file_manager_service.rb, line 8 def base_path @base_path ||= Pathname.new(WarpEngine.config.file_container_path) end
The file container directory, as a Pathname.
Source
# File app/services/warp_engine/file_manager_service.rb, line 61 def delete(relative_path) full = safe_path!(relative_path) raise ArgumentError, "Cannot delete root" if full == base_path if full.directory? full.rmdir else full.delete end end
Deletes the file or the empty directory at relative_path. Raises ArgumentError for the root, and Errno::ENOTEMPTY for a directory that is not empty.
# File app/services/warp_engine/file_manager_service.rb, line 21 def list(relative_path = "", with_download_counts: false) full = safe_path!(relative_path) raise ArgumentError, "Not a directory" unless full.directory? children = full.children.sort_by { |c| [ c.directory? ? 0 : 1, c.basename.to_s.downcase ] } entries = children.map do |child| stat = child.stat { name: child.basename.to_s, path: child.relative_path_from(base_path).to_s, type: child.directory? ? :directory : :file, size: child.directory? ? nil : stat.size, mtime: stat.mtime } end with_download_counts ? add_download_counts(entries) : entries end
Lists the directory relative_path, directories first, then by name.
- relative_path
-
the directory, relative to
base_path(default the root) - with_download_counts
-
also count the
WarpEngine::Downloadrecords of every file
Returns an Array of Hashes with name, path (relative), type (:directory or :file), size (nil for directories), mtime and, when asked, downloads. Raises ArgumentError when the path is not a directory or leaves the container.
# File app/services/warp_engine/file_manager_service.rb, line 90 def mkdir(relative_path, folder_name) parent = safe_path!(relative_path) raise ArgumentError, "Not a directory" unless parent.directory? safe_name = sanitize_name(folder_name) new_dir = parent.join(safe_name) ensure_within_base!(new_dir) new_dir.mkdir new_dir.relative_path_from(base_path).to_s end
Creates the directory folder_name (sanitized) inside relative_path. Returns its relative path. Raises ArgumentError when the parent is not a directory or the name is invalid, and Errno::EEXIST when it exists.
# File app/services/warp_engine/file_manager_service.rb, line 75 def rename(relative_path, new_name) full = safe_path!(relative_path) raise ArgumentError, "Cannot rename root" if full == base_path safe_name = sanitize_name(new_name) new_full = full.parent.join(safe_name) ensure_within_base!(new_full) full.rename(new_full) new_full.relative_path_from(base_path).to_s end
Renames the entry at relative_path to new_name (sanitized) in the same directory. Returns the new relative path. Raises ArgumentError for the root or an invalid name.
# File app/services/warp_engine/file_manager_service.rb, line 45 def upload(relative_dir, uploaded_file) max = WarpEngine.config.max_upload_size raise ArgumentError, "File too large (max #{max / (1024 * 1024)}MB)" if uploaded_file.size > max dir = safe_path!(relative_dir) raise ArgumentError, "Not a directory" unless dir.directory? safe_name = sanitize_name(uploaded_file.original_filename) target = dir.join(safe_name) ensure_within_base!(target) IO.copy_stream(uploaded_file.to_io, target.to_s) target.relative_path_from(base_path).to_s end
Writes uploaded_file (an uploaded file with original_filename, size and to_io) into the directory relative_dir under its sanitized name, overwriting a file of that name. Returns the new fileβs relative path. Raises ArgumentError when the file is larger than WarpEngine.config.max_upload_size, the target is not a directory, or the name is empty or unsafe.