ios-uploader/dcav-uploader/ViewController.swift

126 lines
4.8 KiB
Swift
Raw 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
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate {
@IBOutlet weak var labell: UILabel!
@IBOutlet weak var imageView: UIImageView!
var imagePickerController : UIImagePickerController!
@IBOutlet weak var dcavTextField: UITextField!
var responseString : String!
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
}
@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)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
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))")
}
task.resume()
dcavTextField.text = "https://dcav.pw/\(String(describing: self.labell.text![(self.labell.text!.startIndex)]))\(String(describing: responseString))"
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().uuidString)"
}
func generateRequest(user: String, pass: String) -> URLRequest {
let params : [String : String] = [ "user": user, "pass": pass ]
let url = URL(string: "http://192.168.0.119:35257/upload")
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
}
}