Files icon indicating copy to clipboard operation
Files copied to clipboard

Rename Item API

Open ghost opened this issue 6 years ago • 7 comments

Hi @JohnSundell ,

I'm enjoying the Files API and I'm glad I can help in any way. Even if my contributions are just discussion points that lead to potential pull requests from others.

I have, yet another, discussion point to raise. I hope you don't mind.

RenameItem

Unless I examine the source, it's not clear here if newName is name only or name + extension.

public func rename(to newName: String, keepExtension: Bool = true) throws

Further, I currently have a situation where the above API is inconvenient.

At the moment I'm unpacking a .pdf file from a CKAsset instance (CloudKit).

  1. After digging through the asset, I find a file named asdkfjkajsdkfha.owkheorhjdsf
  2. The file is stored in temp dir so I need to also move it.
  3. I want the file to have a different name, including a new .pdf extension.

Perhaps, we might want to have something like the following?

public func rename(to newName: String) throws

And maybe we can examine the name to see if it contains a "."

Do you think this is a valid issue and do you have any ideas on this?

ghost avatar Jun 01 '18 12:06 ghost

We could remove all ambiguity and assumptions and be more explicit:

enum ExtensionOption {
    case keep
    case remove
    case `extension`(String)
}

public func rename(to name: String, extension: ExtensionOption) throws

And provide a convenience overload that would take a String

public func rename(to name: String, extension: String) throws {
    try rename(to: name, extension: .extension(extension))
}

That way name and extension are both treated as separate parts.

What do you think @rob-nash?

clayellis avatar Jun 01 '18 15:06 clayellis

Ye that's a good idea. How about.

enum ExtensionOption {
    case keep
    case remove
    case change(to: String)
}

Is to: possible ?

ghost avatar Jun 01 '18 16:06 ghost

I like that. Yeah, it's possible. And it's fantastic.

clayellis avatar Jun 01 '18 16:06 clayellis

In order to not break existing code, we'd have to provide a default parameter:

public func rename(to name: String, extension: ExtensionOption = .keep) throws

Which would cover the case where users had used:

try someFile.rename(to: "SomeOtherFile") // without keepExtension

~And then deprecate the previous signature.~ The previous method can be kept around (and would defer to the new method) but its default value of keepExtension = true has to be removed to avoid ambiguity.

clayellis avatar Jun 01 '18 17:06 clayellis

hi @clayellis how might I get in touch with you directly? Are you on twitter?

ghost avatar Jun 11 '18 14:06 ghost

@rob-nash yep! https://twitter.com/clay_ellis DMs are open. Sorry for the late response — just finished packing/moving into a new house.

clayellis avatar Jun 13 '18 18:06 clayellis

Hi @clayellis I think DM's only work if you follow me. I'm not fishing for followers 😜 🙄

Email? [email protected]

ghost avatar Jun 27 '18 19:06 ghost