0

I am having trouble moving two files (3D scanning .obj files) into a directory. I currently have the URL's of the zipped version each of these files, which I originally used to upload them to Firebase separately with no problems.

However, now I am trying to place them into a directory together so I can upload a single file to Firebase. The URLs and the directory are listed below.

I am getting two errors saying that "CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme" and that the "The file couldn’t be opened because the specified URL type isn’t supported". The errors do not seem to be coming from creating the directory but rather from trying to add the files to the directory.

How can I achieve taking the 2 URL's and placing them into the directory I created?

// create directory
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory = paths[0]
    let docURL = URL(string: documentsDirectory)!
    let dataPath = docURL.appendingPathComponent(meshIdentifierFormatted)
    if !FileManager.default.fileExists(atPath: dataPath.absoluteString) {
        do {

            print("creating directory at \(dataPath)")
            try FileManager.default.createDirectory(atPath: dataPath.absoluteString, withIntermediateDirectories: true, attributes: nil)
        } catch {
            print(error.localizedDescription);
        }
    }

Attempt to move files from respective URL into the directory (do the same thing for scanFile2URL)

    // do {
        try FileManager().copyItem(at: scanFile1URL, to: dataPath)
    }
    catch {
        print(error)
    }

scanFile1URL = file:///var/mobile/Containers/Data/Application/B81...A70/Documents/JacksonHubbard-Football-ACJoint-.zip

scanFile2URL = file:///var/mobile/Containers/Data/Application/B81...A70/Documents/JacksonHubbard-Football-ACJoint-2.zip

directory = /var/mobile/Containers/Data/Application/B81...A70/Documents/JacksonHubbard-Football-ACJoint-Scans

  • 1
    You are using the wrong initializer. You should use `URL(fileURLWithPath:)` not `URL(string:)` and also change `dataPath.absoluteString` to `dataPath.path` – Leo Dabus May 03 '20 at 20:50
  • @LeoDabus thank you that worked in correcting the directory errors. Is FileManager().copyItem the correct way to move the files into the directory? It is saying that "the (file) couldn’t be copied to “Documents” because an item with the same name already exists". I assume that is because it exists in the larger documents folder ? because it does not exist in the subdirectory we created yet – Jackson Hubbard May 04 '20 at 00:02
  • No need to create a new FileManager instance every time you call the copyItem method. You should use `FileManager.default.whatever` – Leo Dabus May 04 '20 at 01:00
  • To move an item you can use `moveItem`. You can also use this method to rename a file. Make sure the destination URL it is NOT the directory URL. The destination URL needs to be the actual fileURL (including its name) – Leo Dabus May 04 '20 at 01:01
  • Ok thank you @LeoDabus. I am confused what you mean about the destination URL. I thought the fileURL was what you wanted to move (so for me that would be scanFile1). So I have fileManager.moveItem(at: scanFile1URL, to: dataPath) where dataPath is the directory I want to move the file to. But it is still saying "couldn’t be moved to “Documents” because an item with the same name already exists". What should the destination be instead? – Jackson Hubbard May 04 '20 at 01:18
  • You need to append the name to the dataPath URL – Leo Dabus May 04 '20 at 01:19
  • @LeoDabus like this? do { let newDataPath = dataPath.appendingPathComponent("scan1") try FileManager().moveItem(at: scanFile1URL, to: newDataPath) } catch { print("here2 \(error)") } I then repeat this code and do the same thing for the second scan file (and call it "scan2"). But for the second one it is saying the file couldn’t be moved to the directory because either the former doesn’t exist, or the folder containing the latter doesn’t exist – Jackson Hubbard May 04 '20 at 01:31
  • You can append the lastPathComponemt from your origin URL – Leo Dabus May 04 '20 at 01:33
  • 1
    `dataPath.appendingPathComponent(scanFile1URL.lastPathComponent)` – Leo Dabus May 04 '20 at 01:35
  • 1
    @LeoDabus Ok awesome thanks so much. I think that is working. I will let you know if I have any other questions. Thanks again! – Jackson Hubbard May 04 '20 at 01:56
  • @LeoDabus I am now trying to zip the directory so I can upload that to firebase. When zipping the directory, do I also need to use the .lastPathComponet for the withContentsOfDirectory parameter? I am attempting to use SSZipArchive – Jackson Hubbard May 04 '20 at 03:55
  • You always need to provide a file name (last path component) to the destination archive. I haven’t used SSZipArchive só I don’t know but it looks like it expects a directory URL so it dependes how you are creating the source directory url. – Leo Dabus May 04 '20 at 04:03
  • https://developer.apple.com/documentation/foundation/url/1779992-appendingpathcomponent – Leo Dabus May 04 '20 at 04:08
  • @LeoDabus I am sorry for all of the questions- I have spent a week on this.This is what I am trying. I want to take the directory we added files to above (dataPath) and zip it. SSZipArchive expects a string for both the atPath and withContentsOfDirectory. I have tried changing the dataPath.absoluteString to dataPath.lastPathComponet but it seems that it can't get to the folder without the whole path? `let zipPath = docURL.appendingPathComponent(meshIdentifierFormatted).absoluteString SSZipArchive.createZipFile(atPath: zipPath, withContentsOfDirectory: dataPath.absoluteString)` – Jackson Hubbard May 04 '20 at 04:25
  • 1
    Again if it expects a path you need to pass your fileURL.path not the absoluteString check the duplicate question I have linked to your question when I closed it. – Leo Dabus May 04 '20 at 04:28
  • 1
    You need to pass dataPath.path. Note that naming a url path is totally misleading. You shouldn’t have name it something like folderURL – Leo Dabus May 04 '20 at 04:30
  • @LeoDabus gotcha thank you for that tip. I changed it to `let zipPath = docURL.appendingPathComponent(meshIdentifierFormatted).path` `SSZipArchive.createZipFile(atPath: zipPath, withContentsOfDirectory: dataPath.path)` but when it uploads to Firebase it is a .dms instead of a .zip. The two scanning files are in the directory we created, as I checked the contents of it. Do you have any idea what could prevent it from becoming a .zip and instead being a .dms? – Jackson Hubbard May 04 '20 at 04:44
  • 1
    Imbuiu are not adding the pathExtension “.zip” to your URL – Leo Dabus May 04 '20 at 15:49
  • 1
    @LeoDabus Thank you, I added that and it worked. Thanks so much for your help, I really appreciate it. Have a good week. – Jackson Hubbard May 04 '20 at 18:20

0 Answers0