package com.github._1element.service;

import com.github._1element.domain.Camera;
import com.github._1element.events.ImageReceivedEvent;
import com.github._1element.repository.CameraRepository;
import org.apache.ftpserver.ftplet.DefaultFtplet;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.ftplet.FtpSession;
import org.apache.ftpserver.ftplet.FtpletResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Ftp upload component. Will handle incoming file uploads.
 */
@Component
public class UploadFtplet extends DefaultFtplet {

  @Autowired
  private ApplicationEventPublisher eventPublisher;

  @Autowired
  private CameraRepository cameraRepository;

  @Override
  public FtpletResult onUploadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
    String userRoot = session.getUser().getHomeDirectory();
    String currentDirectory = session.getFileSystemView().getWorkingDirectory().getAbsolutePath();
    String fileArgument = request.getArgument();

    String fileName = userRoot + currentDirectory + fileArgument;
    Camera camera = cameraRepository.findByFtpUsername(session.getUser().getName());

    eventPublisher.publishEvent(new ImageReceivedEvent(fileName, camera));

    return super.onUploadEnd(session, request);
  }

}

@@ -604,16 +604,37 @@
 onent;%0A%0A
+import java.io.File;%0A
 import j
@@ -1290,16 +1290,33 @@
 ectory +
+ File.separator +
  fileArg

Add missing path separator