Skip to content

Commit

Permalink
add Livephoto
Browse files Browse the repository at this point in the history
Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana committed Nov 9, 2023
1 parent 638e3d1 commit e7b19b0
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
12 changes: 12 additions & 0 deletions Sources/NextcloudKit/NKModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,18 @@ class NKDataFileXML: NSObject {
</d:propfind>
"""

let requestBodyLivephoto =
"""
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<d:propertyupdate xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">
<d:set>
<d:prop>
<nc:metadata-files-live-photo>%@</nc:metadata-files-live-photo>
</d:prop>
</d:set>
</d:propertyupdate>
"""

init(nkCommonInstance: NKCommon) {
self.nkCommonInstance = nkCommonInstance
super.init()
Expand Down
70 changes: 70 additions & 0 deletions Sources/NextcloudKit/NextcloudKit+Livephoto.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// NextcloudKit+Livephoto.swift
// NextcloudKit
//
// Created by Marino Faggiana on 09/11/2023.
// Copyright © 2023 Marino Faggiana. All rights reserved.
//
// Author Marino Faggiana <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

import Foundation
import Alamofire

extension NextcloudKit {

public func setLivephoto(fileNamePath: String,
livePhoto: String,
options: NKRequestOptions = NKRequestOptions(),
completion: @escaping (_ account: String, _ error: NKError) -> Void) {

let account = self.nkCommonInstance.account
let urlBase = self.nkCommonInstance.urlBase
let dav = self.nkCommonInstance.dav
let userId = self.nkCommonInstance.userId
let serverUrlEndpoint = urlBase + "/" + dav + "files/" + userId + "/" + fileNamePath

guard let url = serverUrlEndpoint.encodedToUrl else {
return options.queue.async { completion(account, .urlError) }
}

let method = HTTPMethod(rawValue: "PROPPATCH")
let headers = self.nkCommonInstance.getStandardHeaders(options.customHeader, customUserAgent: options.customUserAgent, contentType: "application/xml")

var urlRequest: URLRequest
do {
try urlRequest = URLRequest(url: url, method: method, headers: headers)
let parameters = String(format: NKDataFileXML(nkCommonInstance: self.nkCommonInstance).requestBodyLivephoto, livePhoto)
urlRequest.httpBody = parameters.data(using: .utf8)
} catch {
return options.queue.async { completion(account, NKError(error: error)) }
}

sessionManager.request(urlRequest).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in
if self.nkCommonInstance.levelLog > 0 {
debugPrint(response)
}

switch response.result {
case .failure(let error):
let error = NKError(error: error, afResponse: response, responseData: response.data)
options.queue.async { completion(account, error) }
case .success:
options.queue.async { completion(account, .success) }
}
}
}
}
10 changes: 5 additions & 5 deletions Sources/NextcloudKit/WebDAV/NextcloudKit+WebDAV.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ extension NextcloudKit {

let method = HTTPMethod(rawValue: "PROPFIND")

var headers = self.nkCommonInstance.getStandardHeaders(options.customHeader, customUserAgent: options.customUserAgent, contentType: "text/xml")
var headers = self.nkCommonInstance.getStandardHeaders(options.customHeader, customUserAgent: options.customUserAgent, contentType: "application/xml")
headers.update(name: "Depth", value: depth)

var urlRequest: URLRequest
Expand Down Expand Up @@ -393,7 +393,7 @@ extension NextcloudKit {

let method = HTTPMethod(rawValue: "SEARCH")

let headers = self.nkCommonInstance.getStandardHeaders(options.customHeader, customUserAgent: options.customUserAgent, contentType: "text/xml")
let headers = self.nkCommonInstance.getStandardHeaders(options.customHeader, customUserAgent: options.customUserAgent, contentType: "application/xml")

var urlRequest: URLRequest
do {
Expand Down Expand Up @@ -441,7 +441,7 @@ extension NextcloudKit {

let method = HTTPMethod(rawValue: "PROPPATCH")

let headers = self.nkCommonInstance.getStandardHeaders(options.customHeader, customUserAgent: options.customUserAgent, contentType: "text/xml")
let headers = self.nkCommonInstance.getStandardHeaders(options.customHeader, customUserAgent: options.customUserAgent, contentType: "application/xml")

var urlRequest: URLRequest
do {
Expand Down Expand Up @@ -487,7 +487,7 @@ extension NextcloudKit {

let method = HTTPMethod(rawValue: "REPORT")

let headers = self.nkCommonInstance.getStandardHeaders(options.customHeader, customUserAgent: options.customUserAgent, contentType: "text/xml")
let headers = self.nkCommonInstance.getStandardHeaders(options.customHeader, customUserAgent: options.customUserAgent, contentType: "application/xml")

var urlRequest: URLRequest
do {
Expand Down Expand Up @@ -535,7 +535,7 @@ extension NextcloudKit {

let method = HTTPMethod(rawValue: "PROPFIND")

var headers = self.nkCommonInstance.getStandardHeaders(options.customHeader, customUserAgent: options.customUserAgent, contentType: "text/xml")
var headers = self.nkCommonInstance.getStandardHeaders(options.customHeader, customUserAgent: options.customUserAgent, contentType: "application/xml")
headers.update(name: "Depth", value: "1")

var urlRequest: URLRequest
Expand Down

0 comments on commit e7b19b0

Please sign in to comment.