Browse Source

add class

marinofaggiana 3 years ago
parent
commit
670e9d0222
2 changed files with 105 additions and 0 deletions
  1. 6 0
      Nextcloud.xcodeproj/project.pbxproj
  2. 99 0
      iOSClient/Utility/NCFileChunker.swift

+ 6 - 0
Nextcloud.xcodeproj/project.pbxproj

@@ -52,6 +52,8 @@
 		F70968A424212C4E00ED60E5 /* NCLivePhoto.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70968A324212C4E00ED60E5 /* NCLivePhoto.swift */; };
 		F70A58BE24D0349500DED00D /* NCCapabilitiesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70A58BD24D0349500DED00D /* NCCapabilitiesViewController.swift */; };
 		F70A58C024D0545100DED00D /* NCCapabilitiesViewController.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F70A58BF24D0545100DED00D /* NCCapabilitiesViewController.storyboard */; };
+		F70A89FA26BFE02E00503AC3 /* NCFileChunker.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70A89F926BFE02E00503AC3 /* NCFileChunker.swift */; };
+		F70A89FB26BFE02E00503AC3 /* NCFileChunker.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70A89F926BFE02E00503AC3 /* NCFileChunker.swift */; };
 		F70B866D2642A21300ED5349 /* NCBackgroundImageColor.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F70B866B2642A21300ED5349 /* NCBackgroundImageColor.storyboard */; };
 		F70B866E2642A21300ED5349 /* NCBackgroundImageColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70B866C2642A21300ED5349 /* NCBackgroundImageColor.swift */; };
 		F70B86752642CE3B00ED5349 /* FirebaseCrashlytics in Frameworks */ = {isa = PBXBuildFile; productRef = F70B86742642CE3B00ED5349 /* FirebaseCrashlytics */; };
@@ -444,6 +446,7 @@
 		F70A07C8205285FB00DC1231 /* pt-PT */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "pt-PT"; path = "pt-PT.lproj/Localizable.strings"; sourceTree = "<group>"; };
 		F70A58BD24D0349500DED00D /* NCCapabilitiesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCCapabilitiesViewController.swift; sourceTree = "<group>"; };
 		F70A58BF24D0545100DED00D /* NCCapabilitiesViewController.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = NCCapabilitiesViewController.storyboard; sourceTree = "<group>"; };
+		F70A89F926BFE02E00503AC3 /* NCFileChunker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NCFileChunker.swift; sourceTree = "<group>"; };
 		F70B866B2642A21300ED5349 /* NCBackgroundImageColor.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = NCBackgroundImageColor.storyboard; sourceTree = "<group>"; };
 		F70B866C2642A21300ED5349 /* NCBackgroundImageColor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NCBackgroundImageColor.swift; sourceTree = "<group>"; };
 		F70B866F2642CA9500ED5349 /* ChromaColorPicker.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = ChromaColorPicker.xcframework; path = Carthage/Build/ChromaColorPicker.xcframework; sourceTree = "<group>"; };
@@ -1317,6 +1320,7 @@
 			children = (
 				F7053E3C1C639DF500741EA5 /* CCUtility.h */,
 				F7053E3D1C639DF500741EA5 /* CCUtility.m */,
+				F70A89F926BFE02E00503AC3 /* NCFileChunker.swift */,
 				F733598025C1C188002ABA72 /* NCAskAuthorization.swift */,
 				F765608E23BF813500765969 /* NCContentPresenter.swift */,
 				F702F30725EE5D47008F8E80 /* NCPopupViewController.swift */,
@@ -2026,6 +2030,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				F70A89FB26BFE02E00503AC3 /* NCFileChunker.swift in Sources */,
 				F73D5E48246DE09200DF6467 /* NCElementsJSON.swift in Sources */,
 				F7EDE4E5262D7BBE00414FE6 /* NCSectionHeaderFooter.swift in Sources */,
 				F79EC78926316AC4004E59D6 /* NCPopupViewController.swift in Sources */,
@@ -2206,6 +2211,7 @@
 				F769454822E9F20D000A798A /* NCShareNetworking.swift in Sources */,
 				F7C9555521F0C5470024296E /* NCActivity.swift in Sources */,
 				F7725A60251F33BB00D125E0 /* NCFiles.swift in Sources */,
+				F70A89FA26BFE02E00503AC3 /* NCFileChunker.swift in Sources */,
 				F704B5E52430AA8000632F5F /* NCCreateFormUploadConflict.swift in Sources */,
 				F765608F23BF813600765969 /* NCContentPresenter.swift in Sources */,
 				F70CEF5623E9C7E50007035B /* UIColor+Extensions.swift in Sources */,

+ 99 - 0
iOSClient/Utility/NCFileChunker.swift

@@ -0,0 +1,99 @@
+//
+//  NCFileChunker.swift
+//  Nextcloud
+//
+//  Created by Marino Faggiana on 08/08/21.
+//  Copyright © 2021 Marino Faggiana. All rights reserved.
+//
+//  Author Marino Faggiana <marino.faggiana@nextcloud.com>
+//
+//  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
+
+public class NCFileChunker {
+    
+    let input:URL
+    let outputDirectory:URL
+    let chunkSize:Int
+    let bufferSize:Int
+    
+    init( input:URL, outputDirectory:URL, chunkSize:Int, bufferSize:Int = 1024 ) {
+        self.input = input
+        self.outputDirectory = outputDirectory
+        self.chunkSize = chunkSize
+        self.bufferSize = bufferSize
+    }
+    
+    func chunk( )throws->[URL] {
+        
+        let fileManager:FileManager = .default
+        
+        var isDirectory:ObjCBool = false
+        if !fileManager.fileExists( atPath:outputDirectory.path, isDirectory:&isDirectory ) {
+            try fileManager.createDirectory( at:outputDirectory, withIntermediateDirectories:true, attributes:nil )
+        }
+        
+        var urls:[URL] = [ ]
+        let processInfo:ProcessInfo = .init( )
+        let reader:FileHandle = try .init( forReadingFrom:input )
+        var writer:FileHandle?
+        var buffer:Data?
+        var chunk:Int = 0
+        
+        repeat {
+            
+            if chunk >= chunkSize {
+                writer?.closeFile()
+                //try writer?.close( )
+                writer = nil
+                chunk = 0
+            }
+            
+            
+            let chunkRemaining:Int = chunkSize - chunk
+            buffer = reader.readData(ofLength: min( bufferSize, chunkRemaining ))
+            //buffer = try reader.read( upToCount:min( bufferSize, chunkRemaining ) )
+            
+            if let buffer = buffer {
+                
+                if writer == nil {
+                    
+                    let outputURL = outputDirectory.appendingPathComponent( processInfo.globallyUniqueString ).appendingPathExtension( "chunk" )
+                    fileManager.createFile( atPath:outputURL.path, contents:nil, attributes:nil )
+                    writer = try .init( forWritingTo:outputURL )
+                    urls.append( outputURL )
+                }
+                
+                writer?.write( buffer )
+                chunk = chunk + buffer.count
+            }
+        }
+        while buffer != nil
+        
+        //try reader.close( )
+        reader.closeFile()
+        return urls
+    }
+    
+}
+
+extension URL
+{
+    public func chunk( to outputDirectory:URL, chunkSize:Int, bufferSize:Int = 1024 )throws->[URL] {
+        let chunker:NCFileChunker = .init(input:self, outputDirectory:outputDirectory, chunkSize:chunkSize, bufferSize:bufferSize )
+        return try chunker.chunk( )
+    }
+}