// Path: src/main/java/com/google/homegraph/dashboard/service/StorageService.java
// @Service
// @Scope(value="session")
// public class StorageService implements Serializable{
// 
// 	private static final long serialVersionUID = 1L;
// 	private static Logger log = LoggerFactory.getLogger("StorageService");
// 	private static StorageService instance;
// 
// 	private Map<String, byte[]> inMemoryStore = new HashMap<>();
// 
// 	private StorageService() {}
// 
// 	public static StorageService getInstance() {
// 		if (instance == null) {
// 			instance = new StorageService();
// 		}
// 		return instance;
// 	}
// 
// 	public void storeInMemory(MultipartFile file) {
// 		try {
// 			inMemoryStore.put(file.getOriginalFilename(), convert(file));
// 		} catch (IOException e) {
// 			log.error(e.getMessage() + ", Failed to convert the file and store in memory, will try disk instead");
// 		}
// 	}
// 
// 	public InputStream loadFile(String filename) {
// 		// check if it is in memory
// 		log.debug("Storing file: " + filename);
// 		if (inMemoryStore.containsKey(filename)) {
// 			log.debug("Found file in memory");
// 			return new ByteArrayInputStream(inMemoryStore.get(filename));
// 		} else {
// 			log.debug("File is not in memory");
// 			return null;
// 		}
// 	}
// 
// 	private byte[] convert(MultipartFile file) throws IOException {
// 		InputStream is = file.getInputStream();
// 		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
// 
// 		int nRead;
// 		byte[] data = new byte[16384];
// 		while ((nRead = is.read(data, 0, data.length)) != -1) {
// 			buffer.write(data, 0, nRead);
// 		}
// 
// 		buffer.flush();
// 		return buffer.toByteArray();
// 	}
// }


import com.google.homegraph.dashboard.service.StorageService;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
/**
 * Copyright 2018 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.homegraph.dashboard.controller;



@Controller
@Scope(value="session")
public class UploadController implements Serializable {

	private static final long serialVersionUID = 5546148932202075489L;
	private static Logger log = LoggerFactory.getLogger("UploadController");

	@PostMapping("/upload")
	@CrossOrigin
	public ResponseEntity<String> handleFileUpload(@RequestParam("file") final MultipartFile file) {
// Path: src/main/java/com/google/homegraph/dashboard/service/StorageService.java
// @Service
// @Scope(value="session")
// public class StorageService implements Serializable{
// 
// 	private static final long serialVersionUID = 1L;
// 	private static Logger log = LoggerFactory.getLogger("StorageService");
// 	private static StorageService instance;
// 
// 	private Map<String, byte[]> inMemoryStore = new HashMap<>();
// 
// 	private StorageService() {}
// 
// 	public static StorageService getInstance() {
// 		if (instance == null) {
// 			instance = new StorageService();
// 		}
// 		return instance;
// 	}
// 
// 	public void storeInMemory(MultipartFile file) {
// 		try {
// 			inMemoryStore.put(file.getOriginalFilename(), convert(file));
// 		} catch (IOException e) {
// 			log.error(e.getMessage() + ", Failed to convert the file and store in memory, will try disk instead");
// 		}
// 	}
// 
// 	public InputStream loadFile(String filename) {
// 		// check if it is in memory
// 		log.debug("Storing file: " + filename);
// 		if (inMemoryStore.containsKey(filename)) {
// 			log.debug("Found file in memory");
// 			return new ByteArrayInputStream(inMemoryStore.get(filename));
// 		} else {
// 			log.debug("File is not in memory");
// 			return null;
// 		}
// 	}
// 
// 	private byte[] convert(MultipartFile file) throws IOException {
// 		InputStream is = file.getInputStream();
// 		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
// 
// 		int nRead;
// 		byte[] data = new byte[16384];
// 		while ((nRead = is.read(data, 0, data.length)) != -1) {
// 			buffer.write(data, 0, nRead);
// 		}
// 
// 		buffer.flush();
// 		return buffer.toByteArray();
// 	}
// }


// Path: src/main/java/com/google/homegraph/dashboard/controller/UploadController.java
import com.google.homegraph.dashboard.service.StorageService;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * Copyright 2018 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.homegraph.dashboard.controller;



@Controller
@Scope(value="session")
public class UploadController implements Serializable {

	private static final long serialVersionUID = 5546148932202075489L;
	private static Logger log = LoggerFactory.getLogger("UploadController");

	@PostMapping("/upload")
	@CrossOrigin
	public ResponseEntity<String> handleFileUpload(@RequestParam("file") final MultipartFile file) {
		StorageService storageService = StorageService.getInstance();