Przeglądaj źródła

remove experimental code

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 2 lat temu
rodzic
commit
26700e61e3

+ 7 - 12
Widget/Dashboard/DashboardData.swift

@@ -184,21 +184,16 @@ func getDashboardDataEntry(intent: Applications?, isPreview: Bool, displaySize:
                                         iconFileName = ((path.lastPathComponent) as NSString).deletingPathExtension
                                     }
                                 }
-
-                                do {
-                                    if let fileName = iconFileName {
-                                        let fileNamePath: String = CCUtility.getDirectoryUserData() + "/" + fileName + ".png"
-                                        if FileManager().fileExists(atPath: fileNamePath), let image = UIImage(contentsOfFile: fileNamePath) {
+                                if let fileName = iconFileName {
+                                    let fileNamePath: String = CCUtility.getDirectoryUserData() + "/" + fileName + ".png"
+                                    if FileManager().fileExists(atPath: fileNamePath), let image = UIImage(contentsOfFile: fileNamePath) {
+                                        icon = image
+                                    } else {
+                                        let (_, data, _) = await NCNetworking.shared.getPreview(url: url)
+                                        if let image = NCUtility.shared.convertDataToImage(data: data, size: CGSize(width: 256, height: 256), fileNameToWrite: fileName) {
                                             icon = image
-                                        } else {
-                                            let (_, data) = try await NextcloudKit.shared.getPreview(url: url)
-                                            if let image = NCUtility.shared.convertDataToImage(data: data, size: CGSize(width: 256, height: 256), fileNameToWrite: fileName) {
-                                                icon = image
-                                            }
                                         }
                                     }
-                                } catch {
-                                    print(error)
                                 }
                             }
 

+ 9 - 0
Widget/Files/FilesData.swift

@@ -227,6 +227,14 @@ func getFilesDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escapi
                 if let image = NCUtility.shared.createFilePreviewImage(ocId: file.ocId, etag: file.etag, fileNameView: file.fileName, classFile: file.classFile, status: 0, createPreviewMedia: false) {
                     imageRecent = image
                 } else if file.hasPreview {
+                    let fileNamePathOrFileId = CCUtility.returnFileNamePath(fromFileName: file.fileName, serverUrl: file.serverUrl, urlBase: file.urlBase, account: account.account)!
+                    let fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(file.ocId, etag: file.etag)!
+                    let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(file.ocId, etag: file.etag)!
+                    let (_, _, imageIcon, _, _, _) = await NCNetworking.shared.downloadPreview(fileNamePathOrFileId: fileNamePathOrFileId, fileNamePreviewLocalPath: fileNamePreviewLocalPath, widthPreview: NCGlobal.shared.sizePreview, heightPreview: NCGlobal.shared.sizePreview, fileNameIconLocalPath: fileNameIconLocalPath, sizeIcon: NCGlobal.shared.sizeIcon)
+                    if let image = imageIcon {
+                        imageRecent = image
+                    }
+                    /*
                     do {
                         let fileNamePathOrFileId = CCUtility.returnFileNamePath(fromFileName: file.fileName, serverUrl: file.serverUrl, urlBase: file.urlBase, account: account.account)!
                         let fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(file.ocId, etag: file.etag)!
@@ -238,6 +246,7 @@ func getFilesDataEntry(isPreview: Bool, displaySize: CGSize, completion: @escapi
                     } catch {
                         print(error)
                     }
+                    */
                 }
 
                 // DATA

+ 24 - 112
iOSClient/Networking/NCNetworking.swift

@@ -1462,123 +1462,35 @@ import Photos
         }
     }
 
-    // MARK: - TEST API
-
-    /*
-    func blabla(serverUrl: String, userId: String, account: String) {
-
-        let requestBodyRecent =
-        """
-        <?xml version=\"1.0\"?>
-        <d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">
-        <d:basicsearch>
-            <d:select>
-                <d:prop>
-                    <d:displayname/>
-                    <d:getcontenttype/>
-                    <d:resourcetype/>
-                    <d:getcontentlength/>
-                    <d:getlastmodified/>
-                    <d:getetag/>
-                    <d:quota-used-bytes/>
-                    <d:quota-available-bytes/>
-                    <permissions xmlns=\"http://owncloud.org/ns\"/>
-                    <id xmlns=\"http://owncloud.org/ns\"/>
-                    <fileid xmlns=\"http://owncloud.org/ns\"/>
-                    <size xmlns=\"http://owncloud.org/ns\"/>
-                    <favorite xmlns=\"http://owncloud.org/ns\"/>
-                    <creation_time xmlns=\"http://nextcloud.org/ns\"/>
-                    <upload_time xmlns=\"http://nextcloud.org/ns\"/>
-                    <is-encrypted xmlns=\"http://nextcloud.org/ns\"/>
-                    <mount-type xmlns=\"http://nextcloud.org/ns\"/>
-                    <owner-id xmlns=\"http://owncloud.org/ns\"/>
-                    <owner-display-name xmlns=\"http://owncloud.org/ns\"/>
-                    <comments-unread xmlns=\"http://owncloud.org/ns\"/>
-                    <has-preview xmlns=\"http://nextcloud.org/ns\"/>
-                    <trashbin-filename xmlns=\"http://nextcloud.org/ns\"/>
-                    <trashbin-original-location xmlns=\"http://nextcloud.org/ns\"/>
-                    <trashbin-deletion-time xmlns=\"http://nextcloud.org/ns\"/>
-                </d:prop>
-            </d:select>
-        <d:from>
-            <d:scope>
-                <d:href>%@</d:href>
-                <d:depth>infinity</d:depth>
-            </d:scope>
-        </d:from>
-        <d:where>
-            <d:lt>
-                <d:prop>
-                    <d:getlastmodified/>
-                </d:prop>
-                <d:literal>%@</d:literal>
-            </d:lt>
-        </d:where>
-        <d:orderby>
-            <d:order>
-                <d:prop>
-                    <d:getlastmodified/>
-                </d:prop>
-                <d:descending/>
-            </d:order>
-        </d:orderby>
-        <d:limit>
-            <d:nresults>50</d:nresults>
-        </d:limit>
-        </d:basicsearch>
-        </d:searchrequest>
-        """
-
-        let dateFormatter = DateFormatter()
-        dateFormatter.locale = Locale(identifier: "en_US_POSIX")
-        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
-        let lessDateString = dateFormatter.string(from: Date())
-        let requestBody = String(format: requestBodyRecent, "/files/" + userId, lessDateString)
-
-        NextcloudKit.shared.searchBodyRequest(serverUrl: serverUrl, requestBody: requestBody, showHiddenFiles: CCUtility.getShowHiddenFiles()) { _, files, data, error in
-
-            Task {
-                for file in files {
-                    if file.hasPreview {
-                        let fileNamePath = CCUtility.returnFileNamePath(fromFileName: file.fileName, serverUrl: file.serverUrl, urlBase: file.urlBase, account: account)!
-                        do {
-                            let data = try await self.getPreview(fileNamePath: fileNamePath, urlBase: file.urlBase)
-                            print("AAA")
-                        } catch {
-                            print(error)
-                        }
-                    }
-                }
-                print("END")
-            }
-        }
-    }
-    func getPreview(fileNamePath: String, urlBase: String, options: NKRequestOptions = NKRequestOptions()) async throws -> Data? {
+    // MARK: - NextcloudKit convert completion handlers into async functions
 
-        try await withUnsafeThrowingContinuation { continuation in
-
-            guard let fileNamePath = fileNamePath.urlEncoded else {
-                return
-            }
-            let headers = NKCommon.shared.getStandardHeaders(options: options)
-            let endpoint = "index.php/core/preview.png?file=\(fileNamePath)&x=512&y=512&a=1&mode=cover"
-            guard let url = NKCommon.shared.createStandardUrl(serverUrl: urlBase, endpoint: endpoint) else {
-                return
+    func getPreview(url: URL,
+                    options: NKRequestOptions = NKRequestOptions()) async -> (account: String, data: Data?, error: NKError) {
+        
+        await withUnsafeContinuation({ continuation in
+            NextcloudKit.shared.getPreview(url: url, options: options) { account, data, error in
+                continuation.resume(returning: (account: account, data: data, error: error))
             }
+        })
+    }
 
-            AF.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: NKCommon.shared.backgroundQueue) { (response) in
-                debugPrint(response)
-                
-                switch response.result {
-                case .failure(let error):
-                    continuation.resume(throwing: error)
-                case .success(let data):
-                    continuation.resume(returning: data)
-                }
+    func downloadPreview(fileNamePathOrFileId: String,
+                         fileNamePreviewLocalPath: String,
+                         widthPreview: Int,
+                         heightPreview: Int,
+                         fileNameIconLocalPath: String? = nil,
+                         sizeIcon: Int = 0,
+                         etag: String? = nil,
+                         endpointTrashbin: Bool = false,
+                         useInternalEndpoint: Bool = true,
+                         options: NKRequestOptions = NKRequestOptions()) async -> (account: String, imagePreview: UIImage?, imageIcon: UIImage?, imageOriginal: UIImage?, etag: String?, error: NKError) {
+
+        await withUnsafeContinuation({ continuation in
+            NextcloudKit.shared.downloadPreview(fileNamePathOrFileId: fileNamePathOrFileId, fileNamePreviewLocalPath: fileNamePreviewLocalPath, widthPreview: widthPreview, heightPreview: heightPreview, fileNameIconLocalPath: fileNameIconLocalPath, sizeIcon: sizeIcon, etag: etag, options: options) { account, imagePreview, imageIcon, imageOriginal, etag, error in
+                continuation.resume(returning: (account: account, imagePreview: imagePreview, imageIcon: imageIcon, imageOriginal: imageOriginal, etag: etag, error: error))
             }
-        }
+        })
     }
-    */
 }
 
 extension Array where Element == URLQueryItem {

+ 2 - 4
iOSClient/Networking/NCService.swift

@@ -278,11 +278,9 @@ class NCService: NSObject {
                     NCManageDatabase.shared.addDashboardWidget(account: account, dashboardWidgets: dashboardWidgets)
                     for widget in dashboardWidgets {
                         if let url = URL(string: widget.iconUrl), let fileName = widget.iconClass {
-                            do {
-                                let (_, data) = try await NextcloudKit.shared.getPreview(url: url)
+                            let (_, data, error) = await NCNetworking.shared.getPreview(url: url)
+                            if error == .success {
                                 NCUtility.shared.convertDataToImage(data: data, size: CGSize(width: 256, height: 256), fileNameToWrite: fileName)
-                            } catch {
-                                print(error)
                             }
                         }
                     }