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-08-01 11:38:18 +00:00
|
|
|
@IBOutlet weak var savedPhotoImageView: UIImageView!
|
2017-08-01 12:39:45 +00:00
|
|
|
let pasteBoard = UIPasteboard.general
|
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)
|
2017-08-01 12:39:45 +00:00
|
|
|
imageView.contentMode = .scaleAspectFit
|
2017-07-31 21:52:46 +00:00
|
|
|
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
|
2017-08-07 19:03:12 +00:00
|
|
|
|
|
|
|
let image : UIImage = imageView.image!;
|
|
|
|
var tempImage : UIImage? = nil;
|
|
|
|
let targetSize : CGSize = CGSize(width: 1080, height: 1920);
|
|
|
|
UIGraphicsBeginImageContext(targetSize);
|
|
|
|
|
|
|
|
var thumbnailRect : CGRect = CGRect(x:0, y:0, width:0, height:0);
|
|
|
|
thumbnailRect.origin = CGPoint(x:0.0,y:0.0);
|
|
|
|
thumbnailRect.size.width = targetSize.width;
|
|
|
|
thumbnailRect.size.height = targetSize.height;
|
|
|
|
|
|
|
|
|
|
|
|
image.draw(in: thumbnailRect)
|
|
|
|
tempImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
|
|
|
|
|
|
UIGraphicsEndImageContext();
|
|
|
|
|
|
|
|
imageView.image = tempImage;
|
|
|
|
|
2017-07-31 21:52:46 +00:00
|
|
|
}
|
2017-07-31 22:51:28 +00:00
|
|
|
|
|
|
|
func setTextInField(path: String) {
|
|
|
|
dcavTextField.text = path
|
|
|
|
}
|
2017-07-31 21:52:46 +00:00
|
|
|
|
|
|
|
@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))")
|
2017-07-31 22:51:28 +00:00
|
|
|
DispatchQueue.main.async() {
|
2017-08-01 12:39:45 +00:00
|
|
|
self.setTextInField(path: "https://dcav.pw/\(String(describing: self.labell.text![(self.labell.text!.startIndex)]))\(String(describing: self.responseString!))")
|
|
|
|
self.pasteBoard.string = self.dcavTextField.text!
|
2017-07-31 22:51:28 +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
|
|
|
|
2017-08-01 12:15:59 +00:00
|
|
|
@IBAction func onSaveButton(_ sender: UIButton) {
|
|
|
|
|
|
|
|
UIImageWriteToSavedPhotosAlbum(imageView.image!, nil, nil, nil);
|
|
|
|
|
|
|
|
self.savedPhotoImageView.alpha = 0
|
|
|
|
self.savedPhotoImageView.isHidden = false
|
|
|
|
|
|
|
|
UIView.animate(withDuration: 0.3, delay:0.0, options:UIViewAnimationOptions.transitionCrossDissolve, animations: {
|
|
|
|
|
|
|
|
self.savedPhotoImageView.alpha = 1
|
|
|
|
|
|
|
|
}, completion: { finished in
|
|
|
|
UIView.animate(withDuration: 0.3, delay:1, options:UIViewAnimationOptions.transitionCrossDissolve, animations: {
|
|
|
|
self.savedPhotoImageView.alpha = 0
|
|
|
|
|
|
|
|
}, completion: { finished in
|
|
|
|
self.savedPhotoImageView.isHidden = true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|