Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constructFormattedURL / needs a rethink #96

Closed
johndpope opened this issue Jul 26, 2021 · 3 comments
Closed

constructFormattedURL / needs a rethink #96

johndpope opened this issue Jul 26, 2021 · 3 comments

Comments

@johndpope
Copy link

When I was using digger - it used this technique to simplify the name of url.
N.B. it's using md5.

static func tempPath(url: URL ) -> String {

    return url.absoluteString.md5.tmpDir
}

internal func constructFormattedURL(from sourceURL: URL, expectedDirectoryURL: URL, size: CGSize?) -> URL {

    // size: CGSize(width: 327.0, height: 246.0)
    // image name 1: URL: https://picsum.photos/id/0/5616/3744 -> becomes -> 3744-size-327.0-246.0 (no extension)
    // image name 2: URL: https://server/url/to/your/image.png -> becomes -> image-size-327-0-246-0.png
    
    var formattedFileName = sourceURL.localUniqueFileName()
    let fileExtension = sourceURL.pathExtension
    
    if let size = size {
        let width = String(describing: size.width).replacingOccurrences(of: ".", with: "-")
        let height = String(describing: size.height).replacingOccurrences(of: ".", with: "-")
        let sizePathComponent: String = "-size-\(width)-\(height)"
        
        formattedFileName += sizePathComponent
    }
    
    let sizeFormattedURL = expectedDirectoryURL
        .appendingPathComponent(formattedFileName)
        .appendingPathExtension(fileExtension)
    
    DebugLogger.shared.addDebugMessage("\(String(describing: type(of: self))) - Constructing formatted URL: \(sizeFormattedURL)")
    return sizeFormattedURL. /// WE NEED TO USE md5 here.
}

urlVideoPlayerIsReadyToPlay :Optional("https://d21m91m763fb64.cloudfront.net/videos-360p/a9731a30-a15f-11eb-8337-67b105f10dfa-.mp4")
2021-07-26 11:14:09.726182+1000 8secondz[3502:1036162] DownloadTaskManager - finished download for url: https://d21m91m763fb64.cloudfront.net/videos-360p/a9731a30-a15f-11eb-8337-67b105f10dfa-.mp4 - local file name: https:__d21m91m763fb64.cloudfront.net_videos-360p_a9731a30-a15f-11eb-8337-67b105f10dfa-. DownloadTask: BackgroundDownloadTask <14777A73-6827-45B1-BFC4-D3B32376EA46>.<1>. Temporary file location: file:///private/var/mobile/Containers/Data/Application/ABAE01F4-0A9D-487E-A39F-606A81A234FA/Library/Caches/com.apple.nsurlsessiond/Downloads/app.8secondz.ios/CFNetworkDownload_EuG8OL.tmp
2021-07-26 11:14:09.728414+1000 8secondz[3502:1036162] FileStorageManager - Attempting to delete file for path: /private/var/mobile/Containers/Data/Application/ABAE01F4-0A9D-487E-A39F-606A81A234FA**/tmp/https:__d21**m91m763fb64.cloudfront.net_videos-360p_a9731a30-a15f-11eb-8337-67b105f10dfa-.mp4
2021-07-26 11:14:09.731695+1000 8secondz[3502:1036162] DownloadTaskManager - Moved to intermediate temporary file url: file:///private/var/mobile/Containers/Data/Application/ABAE01F4-0A9D-487E-A39F-606A81A234FA/tmp/https:__d21m91m763fb64.cloudfront.net_videos-360p_a9731a30-a15f-11eb-8337-67b105f10dfa-.mp4
2021-07-26 11:14:09.733670+1000 8secondz[3502:1036162] FileStorageManager - Constructing formatted URL: file:///var/mobile/Containers/Data/Application/ABAE01F4-0A9D-487E-A39F-606A81A234FA/Documents/Celestial/CachedVideos/https:__d21m91m763fb64.cloudfront.net_videos-360p_a9731a30-a15f-11eb-8337-67b105f10dfa-.mp4
2021-07-26 11:14:09.734680+1000 8secondz[3502:1036162] FileStorageManager - Attempting to delete file for path: /var/mobile/Containers/Data/Application/ABAE01F4-0A9D-487E-A39F-606A81A234FA/Documents/Celestial/CachedVideos/https:__d21m91m763fb64.cloudfront.net_videos-360p_a9731a30-a15f-11eb-8337-67b105f10dfa-.mp4
2021-07-26 11:14:09.740544+1000 8secondz[3502:1036162] Optional - Error storing the downloaded video to the file cache Error Domain=NSCocoaErrorDomain Code=4 "“https/__d21m91m763fb64.cloudfront.net_videos-360p_a9731a30-a15f-11eb-8337-67b105f10dfa-.mp4” couldn’t be moved to “CachedVideos” because either the former doesn’t exist, or the folder containing the latter doesn’t exist."

@8secz-johndpope
Copy link
Contributor

8secz-johndpope commented Jul 26, 2021

  func convertURLToUniqueFileName() -> String {
        guard let _ = URL(string: self) else {
            fatalError("\(self) is not a valid URL")
        }
        return self.md5
    }
    

@johndpope
Copy link
Author

on an out of date branch.

@8secz-johndpope
Copy link
Contributor

8secz-johndpope commented Jul 26, 2021

I think using lastPathComponent + md5 is better way to handle filenames.

https://blablabla.com/test.mp4 -> test.mp4 -> 123412341234123412343
sourceURL.lastPathComponent.convertURLToUniqueFileName()

func convertURLToUniqueFileName() -> String {
guard let _ = URL(string: self) else {
fatalError("(self) is not a valid URL")
}
return self.md5
}

It's possible the urls will be too large for file system.

/tmp/. Directory contents: ["httpsd21m91m763fb64.cloudfront.netvideos-540p19f1efb0-8a1e-11eb-8bc7-65134f4b7bab-.mp4", "https:__d21m91m763fb64.cloudfront.net_videos-540p_d9379510-903f-11eb-9a88-f5ff80ee2719-.mp4", "https:__d21m91m763fb64.cloudfront.net_videos-540p_9edc1cd0-a9f1-11eb-b98a-69bcf457ca9a-.mp4", "httpsd21m91m763fb64.cloudfront.netvideos-540pe4b390e0-8560-11eb-822b-3bb0ffb6770c-.mp4", "https:__d21m91m763fb64.cloudfront.net_videos-540p_22114af0-82cc-11eb-a662-35cb0ee94b96-.mp4", "stack-logs.3760.10e1dc000.8secondz.8SFCWk.index", "https:__d21m91m763fb64.cloudfront.net_videos-540p_51180470-9856-11eb-968f-297a20a6926d-.mp4", "instrument", "https:__d21m91m763fb64.cloudfront.net_videos-540p_b28a8d60-7dfb-11eb-aff5-433e586b7aee-.mp4

I can't explain why - but I'm seeing inexplicable failure to move the file from tmp to Celestial folder.

I'll draft a new branch with last path component for your review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants