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

fix(publicShare): Audio preview #1403

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions kDrive/UI/Controller/Files/Preview/PreviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,23 @@ final class PreviewViewController: UIViewController, PreviewContentCellDelegate,
return
}

downloadFile(at: indexPath)
}

private func downloadFile(at indexPath: IndexPath) {
if let publicShareProxy = driveFileManager.publicShareProxy {
downloadPublicShareFile(at: indexPath, publicShareProxy: publicShareProxy)
} else {
downloadFile(at: indexPath)
DownloadQueue.instance.temporaryDownload(
file: currentFile,
userId: accountManager.currentUserId,
onOperationCreated: { operation in
self.trackOperationCreated(at: indexPath, downloadOperation: operation)
},
completion: { error in
self.downloadCompletion(at: indexPath, error: error)
}
)
}
}

Expand All @@ -568,19 +581,6 @@ final class PreviewViewController: UIViewController, PreviewContentCellDelegate,
)
}

private func downloadFile(at indexPath: IndexPath) {
DownloadQueue.instance.temporaryDownload(
file: currentFile,
userId: accountManager.currentUserId,
onOperationCreated: { operation in
self.trackOperationCreated(at: indexPath, downloadOperation: operation)
},
completion: { error in
self.downloadCompletion(at: indexPath, error: error)
}
)
}

private func trackOperationCreated(at indexPath: IndexPath, downloadOperation: DownloadAuthenticatedOperation?) {
Task { @MainActor [weak self] in
guard let self else {
Expand Down
36 changes: 24 additions & 12 deletions kDriveCore/AudioPlayer/SingleTrackPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,39 @@ public final class SingleTrackPlayer: Pausable {
Task {
await setMetaData(from: asset.commonMetadata, playableFileName: playableFile.name)
}
} else if let publicShareProxy = driveFileManager.publicShareProxy {
let url = Endpoint.downloadShareLinkFile(
driveId: publicShareProxy.driveId,
linkUuid: publicShareProxy.shareLinkUid,
fileId: playableFile.id
).url
let asset = AVURLAsset(url: url)
setupStreamingAsset(asset, fileName: playableFile.name)
} else if let token = driveFileManager.apiFetcher.currentToken {
driveFileManager.apiFetcher.performAuthenticatedRequest(token: token) { token, _ in
guard let token else { return }
Task { @MainActor in
let url = Endpoint.download(file: playableFile).url
let headers = ["Authorization": "Bearer \(token.accessToken)"]
let asset = AVURLAsset(url: url, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
self.player = AVPlayer(playerItem: AVPlayerItem(asset: asset))
await self.setMetaData(from: asset.commonMetadata, playableFileName: playableFile.name)
self.setUpObservers()

self.currentItemStatusObserver = self.player?.observe(\.currentItem?.status) { _, _ in
self.handleItemStatusChange()
}
}
let url = Endpoint.download(file: playableFile).url
let headers = ["Authorization": "Bearer \(token.accessToken)"]
let asset = AVURLAsset(url: url, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
self.setupStreamingAsset(asset, fileName: playableFile.name)
}
} else {
onPlaybackError.send(.previewLoadErrorNoToken)
}
}

private func setupStreamingAsset(_ urlAsset: AVURLAsset, fileName: String) {
Task { @MainActor in
player = AVPlayer(playerItem: AVPlayerItem(asset: urlAsset))
await setMetaData(from: urlAsset.commonMetadata, playableFileName: fileName)
setUpObservers()

currentItemStatusObserver = player?.observe(\.currentItem?.status) { _, _ in
self.handleItemStatusChange()
}
}
}

public func reset() {
removeAllObservers()
player?.pause()
Expand Down
Loading