0

Here is my situation: i'm calling file locally on my ios application ( Running in Swift).

If the file is a jpg, one action happen, if the file is a mp4, another action happen.

For this i'musing this code:

    let urlString = "\(posts[selectedIndexPath].link)"

    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

    let fileName = urlString as NSString;
    let filePath="\(documentsPath)/\(fileName.lastPathComponent)";
    let fileURL = NSURL.init(fileURLWithPath: filePath)
    let request = NSURLRequest.init(url: fileURL as URL)


    /* END DOWNLOAD + READ LOCALY */

    if (fileURL.pathExtension?.hasPrefix("jpg"))! {
        Swift.print("THIS IS A JPG")


    }
    else if (fileURL.pathExtension == "mp4") {

        Swift.print("THIS IS A MP4")

    }

This works perfectly.

What i need to do now is instead of calling th eifle locally, to calling it form an URL.

I read my file from an url by:

videoVRView.load(from: URL(string: "\(posts[selectedIndexPath].link)")

Which work.

But from that, the action is not working, i've try the following:

 if ((from: URL(string: "\(posts[selectedIndexPath].link)").hasPrefix("jpg"))! {
    Swift.print("THIS IS A JPG")


}
else if ((from: URL(string: "\(posts[selectedIndexPath].link)") == "mp4") {

    Swift.print("THIS IS A MP4")

}

Without any success !!

Does anybody know how is this achievable ?

Thanks a lot =)

-- EDIT --

What im trying to do is th efollowing to resume:

at th emoment i call image locally via:

       imageVRView.load(UIImage(named: "\(documentsPath)/\(fileName.lastPathComponent)" ),
   of: GVRPanoramaImageType.stereoOverUnder)

I try instead to use:

imageVRView.load(UIImage(named: "\(posts[selectedIndexPath].link)" ),
   of: GVRPanoramaImageType.stereoOverUnder)

Without success . . . . I need to call the image via this method ... any idea ? Thanks a lot !

tibewww
  • 593
  • 4
  • 11
  • 32
  • 1
    `hasPrefix` tests for patterns like `jpgFile` and `mp4Stream`. You need to look for suffix, not prefix. [`hasSuffix`](https://developer.apple.com/reference/swift/string/1541149-hassuffix). – Sergey Kalinichenko May 05 '17 at 11:14
  • Show the value of `posts[selectedIndexPath].link`. – shallowThought May 05 '17 at 11:16
  • You should check the url UTI type for local resources or the mimeType, `if mimeType.hasPrefix("image")` from the url response if not local. – Leo Dabus May 05 '17 at 11:18
  • @shallowThought the value is just a normal url such as www.mydomain.com/image.jpg – tibewww May 05 '17 at 11:19
  • @LeoDabus How to integrate the MIMEtype in my code do you think ? – tibewww May 05 '17 at 11:19
  • you can check the syntax at this answer http://stackoverflow.com/a/27712427/2303865 – Leo Dabus May 05 '17 at 11:20
  • `www.mydomain.com/image.jpg` does not start with (prefix) `jpg`. Take @dasblinkenlights advice. – shallowThought May 05 '17 at 11:22
  • @shallowThought not every image link ends with the file type. for instance every dropbox link ends with dl=0 or dl=1 `https://www.dropbox.com/s/sk46eyglvijlrec/horse.jpg?dl=1` – Leo Dabus May 05 '17 at 11:24
  • @shallowThought im using firebase so path is like https://firebasestorage.googleapis.com/v0/b/mydomain.appspot.com/o/01_07.jpeg?alt=media&token=c1c-8f77-b699904a475d – tibewww May 05 '17 at 11:26

1 Answers1

0

You can make a URL request for the url header using the httpMethod HEAD to check your url mime type without the need to download the data first:

let link = "https://www.dropbox.com/s/sk46eyglvijlrec/horse.jpg?dl=1"
let url = URL(string: link)!

var request = URLRequest(url: url)
request.httpMethod = "HEAD"
URLSession.shared.dataTask(with: request) { _ , response , _ in
    guard let response = response, (response as? HTTPURLResponse)?.statusCode == 200 else { return }
    DispatchQueue.main.async() {
        print("mimeType", response.mimeType ?? "nil")            // image/jpeg
        print("suggestedFilename:", response.suggestedFilename ?? "no suggestedFilename")   // horse.jpg
        print("expectedContentLength:", response.expectedContentLength ?? "nil")   // 352614
        print("textEncodingName:", response.textEncodingName ?? "nil")
        print("url:", response.url ?? "nil")                 // "https://dl.dropboxusercontent.com/content_link/RNrhGtvroTLU1Gww7eQo1N1ePRiix68zsqZJ1xWPjKm3pmOUNQwNVntbPuFG4jZ8/file?dl=1"
    }
}.resume()
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • thanks, seems i have no luck I'm using similar way at the beginning of my code .. . I've edit my questions, maybe it will help ? what i need is call url of an image via name method . .. Thanks a lot ! – tibewww May 05 '17 at 16:19