package com.royalrangers.service;

import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.v2.DbxClientV2;
import com.dropbox.core.v2.files.FileMetadata;
import com.dropbox.core.v2.sharing.SharedLinkMetadata;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.UUID;

@Service
public class DropboxService {
    @Value("${dropbox.appName}")
    private String appName;
    @Value("${dropbox.accessToken}")
    private String accessToken;

    public DbxClientV2 getClient() {
    DbxRequestConfig config = new DbxRequestConfig(appName, Locale.getDefault().toString());
    DbxClientV2 client = new DbxClientV2(config, accessToken);
        return client;
    }

    public void fileUpload(MultipartFile file) throws IOException, DbxException {
        byte[] bytes = file.getBytes();
        InputStream in = new ByteArrayInputStream(bytes);
        FileMetadata metadata = getClient().files().uploadBuilder("/" + file.getOriginalFilename())
                .uploadAndFinish(in);
    }

    public String avatarUpload(MultipartFile file) throws IOException, DbxException {

        String path = "/avatar/";
        String extension = getFilenameExtension(file.getOriginalFilename());
        String filename = UUID.randomUUID().toString() + extension;

        InputStream in = file.getInputStream();
        DbxClientV2 client = getClient();

        FileMetadata metadata = client.files().uploadBuilder(path + filename).uploadAndFinish(in);
        SharedLinkMetadata shared = client.sharing().createSharedLinkWithSettings(path +filename);

        return shared.getUrl();
    }

    private String getFilenameExtension (String filename) {
        return filename.substring(filename.lastIndexOf("."));
    }
 }

@@ -791,20 +791,16 @@
 config =
- new
  DbxRequ
@@ -812,18 +812,57 @@
 nfig
-(appName, 
+.newBuilder(appName)%0A            .withUserLocale(
 Loca
@@ -888,16 +888,37 @@
 tring())
+%0A            .build()
 ;%0A    Db

Replace deprecated "new DbxRequestConfig"