<commit_before>//
//  FileServiceProvider.swift
//  Photostream
//
//  Created by <NAME> on 17/08/2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import FirebaseStorage
import FirebaseDatabase

struct FileServiceProvider: FileService {

    var session: AuthSession

    init(session: AuthSession) {
        self.session = session
    }
    
    func uploadJPEGImage(data: FileServiceUploadData, width: Float, height: Float, callback: ((FileServiceResult) -> Void)?) {
        var result = FileServiceResult()
        result.uploadId = data.id
        guard session.isValid else {
            result.error = .authenticationNotFound(message: "Authentication not found.")
            callback?(result)
            return
        }
        
        guard let imageData = data.data else {
            result.error = .noDataToUpload(message: "No data to upload.")
            callback?(result)
            return
        }
        
        let userId = session.user.id
        let storageRef = FIRStorage.storage().reference()
        let key = Int(Date.timeIntervalSinceReferenceDate * 1000)
        let imagePath = "\(userId)/posts/\(key).jpg"
        let metadata = FIRStorageMetadata()
        metadata.contentType = "image/jpeg"
        
        storageRef.child(imagePath).put(imageData, metadata: metadata, completion: { (metadata, error) in
            guard error != nil else {
                result.error = .failedToUpload(message: "Failed upload JPEG image.")
                callback?(result)
                return
            }
            
            guard let imageUrl = metadata?.downloadURL()?.absoluteString else {
                result.error = .failedToUpload(message: "Image URL does not exist.")
                callback?(result)
                return
            }
            
            let databaseRef = FIRDatabase.database().reference()
            let key = databaseRef.child("photos").childByAutoId().key
            let path = "photos/\(key)"
            let data: [String: AnyObject] = [
                "id": key as AnyObject,
                "uid": userId as AnyObject,
                "url": imageUrl as AnyObject,
                "height": height as AnyObject,
                "width": width as AnyObject ]
            databaseRef.child(path).setValue(data)
            
            result.fileId = key
            result.fileUrl = imageUrl
            callback?(result)
        })
    }
}<commit_msg>Included observing of task's progress.<commit_after>//
//  FileServiceProvider.swift
//  Photostream
//
//  Created by <NAME> on 17/08/2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit
import FirebaseStorage
import FirebaseDatabase

struct FileServiceProvider: FileService {

    var session: AuthSession

    init(session: AuthSession) {
        self.session = session
    }
    
    func uploadJPEGImage(data: FileServiceImageUploadData, track: ((Progress?) -> Void)?, callback: ((FileServiceResult) -> Void)?) {
        var result = FileServiceResult()
        result.uploadId = data.id
        guard session.isValid else {
            result.error = .authenticationNotFound(message: "Authentication not found.")
            callback?(result)
            return
        }
        
        guard let imageData = data.data else {
            result.error = .noDataToUpload(message: "No data to upload.")
            callback?(result)
            return
        }
        
        let userId = session.user.id
        let storageRef = FIRStorage.storage().reference()
        let key = Int(Date.timeIntervalSinceReferenceDate * 1000)
        let imagePath = "\(userId)/posts/\(key).jpg"
        let metadata = FIRStorageMetadata()
        metadata.contentType = "image/jpeg"
        
        let task = storageRef.child(imagePath).put(imageData, metadata: metadata, completion: { (metadata, error) in
            guard error == nil else {
                result.error = .failedToUpload(message: "Failed upload JPEG image.")
                callback?(result)
                return
            }
            
            guard let imageUrl = metadata?.downloadURL()?.absoluteString else {
                result.error = .failedToUpload(message: "Image URL does not exist.")
                callback?(result)
                return
            }
            
            let databaseRef = FIRDatabase.database().reference()
            let key = databaseRef.child("photos").childByAutoId().key
            let path = "photos/\(key)"
            let data: [String: AnyObject] = [
                "id": key as AnyObject,
                "uid": userId as AnyObject,
                "url": imageUrl as AnyObject,
                "height": data.height as AnyObject,
                "width": data.width as AnyObject ]
            databaseRef.child(path).setValue(data)
            
            result.fileId = key
            result.fileUrl = imageUrl
            callback?(result)
        })
        
        guard track != nil else {
            return
        }
        
        task.observe(.progress) { (snapshot) in
            track!(snapshot.progress)
        }
    }
}
{'fraction_non_alphanumeric': 0.09992239037640667, 'fraction_numerical': 0.006208769887466045, 'mean_word_length': 1.7611140867702195, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '373190', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1281, 'n_words': 332}