ios-uploader/dcav-uploader/ViewController.swift

140 lines
5.5 KiB
Swift
Raw Permalink Normal View History

2017-07-31 21:52:46 +00:00
//
// ViewController.swift
// dcav-uploader
//
// Created by Alexander Munch-hansen on 31/07/2017.
// Copyright © 2017 Guava. All rights reserved.
//
import UIKit
2017-07-31 23:20:18 +00:00
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate, URLSessionDelegate, URLSessionTaskDelegate, URLSessionDataDelegate {
2017-07-31 21:52:46 +00:00
@IBOutlet weak var labell: UILabel!
@IBOutlet weak var imageView: UIImageView!
var imagePickerController : UIImagePickerController!
@IBOutlet weak var dcavTextField: UITextField!
var responseString : String!
2017-07-31 23:20:18 +00:00
@IBOutlet weak var uploadProgressBar: UIProgressView!
2017-07-31 21:52:46 +00:00
override func viewDidLoad() {
super.viewDidLoad()
UserDefaults.standard.synchronize()
dcavTextField.delegate = self
labell.text = UserDefaults.standard.string(forKey: "username_preference")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func onLibraryButton(_ sender: Any) {
imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = .photoLibrary
present(imagePickerController, animated: true, completion: nil)
}
@IBAction func onPhotoButton(_ sender: Any) {
imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = .camera
present(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePickerController.dismiss(animated: true, completion: nil)
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
func setTextInField(path: String) {
dcavTextField.text = path
}
2017-07-31 21:52:46 +00:00
@IBAction func onSaveButton(_ sender: Any) {
}
@IBAction func onUploadButton(_ sender: Any) {
guard let username : String = UserDefaults.standard.string(forKey: "username_preference") else {
print("USERNAME, MOTHERFUCKER!")
// TODO create proper pop-up
return
}
guard let passphrase : String = UserDefaults.standard.string(forKey: "passphrase_preference") else {
// TODO create proper pop-up
print("PASSPHRASE, MOTHERFUCKER!")
return
}
let request = generateRequest(user: username, pass: passphrase)
2017-07-31 23:20:18 +00:00
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)
let task = session.dataTask(with: request) { data, response, error in
2017-07-31 21:52:46 +00:00
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
self.responseString = String(data: data, encoding: .utf8)!
print("responseString = \(String(describing: self.responseString))")
DispatchQueue.main.async() {
self.setTextInField(path: "https://dcav.pw/\(String(describing: self.labell.text![(self.labell.text!.startIndex)]))\(String(describing: self.responseString!))")
}
2017-07-31 21:52:46 +00:00
}
2017-07-31 22:25:17 +00:00
2017-07-31 21:52:46 +00:00
task.resume()
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().uuidString)"
}
func generateRequest(user: String, pass: String) -> URLRequest {
let params : [String : String] = [ "user": user, "pass": pass ]
2017-07-31 22:25:17 +00:00
let url = URL(string: UserDefaults.standard.string(forKey: "endpoint_preference")!)
2017-07-31 21:52:46 +00:00
let boundary = generateBoundaryString()
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let imageData: Data = UIImagePNGRepresentation(imageView.image!)!
var body = Data()
for (key, value) in params {
body.append(Data("--\(boundary)\r\n".utf8))
body.append(Data("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".utf8))
body.append(Data("\(value)\r\n".utf8))
}
let mimetype : String = "application/octet-stream"
body.append(Data("--\(boundary)\r\n".utf8))
body.append(Data("Content-Disposition: form-data; name=\"imagedata\"; filename=\"lolfag.png\"\r\n".utf8))
body.append(Data("Content-Type: \(mimetype)\r\n\r\n".utf8))
body.append(imageData)
body.append(Data("\r\n".utf8))
body.append(Data("--\(boundary)--\r\n".utf8))
request.httpBody = body
return request
}
2017-07-31 23:20:18 +00:00
//MARK:Update progress bar
func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
uploadProgressBar.progress = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
print(String(describing: uploadProgressBar.progress))
}
2017-07-31 21:52:46 +00:00
}