import { toTextFile } from '../tools/to-file';
import { ConvertOpts, uploadFileChunked } from './upload-chunk';
/**
 * 对创建的directory的路径进行解析，
 * 如果是 nameA/nameB/nameC 则创建 nameA/nameB/nameC
 *
 * 不能以.开头，不能以.结尾，不能以/开头，不能以/结尾。
 * @param directory
 * @param opts
 * @returns
 */
export const createDirectory = async (directory: string = '', opts?: ConvertOpts) => {
  const directoryPath = directory || opts?.directory;
  const error = '目录名不能以.开头，不能以.结尾，不能以/开头，不能以/结尾，不能包含//';
  const errorMsg = () => {
    return {
      code: 400,
      message: error,
      success: false,
    };
  };
  if (directoryPath) {
    if (directoryPath.startsWith('.')) {
      return errorMsg();
    }
    if (directoryPath.endsWith('.')) {
      return errorMsg();
    }
    if (directoryPath.startsWith('/')) {
      return errorMsg();
    }
    if (directoryPath.endsWith('/')) {
      return errorMsg();
    }
    if (directoryPath.includes('//')) {
      return errorMsg();
    }
  }
  const res = await uploadFileChunked(toTextFile('keep directory exist', 'keep.txt'), {
    directory,
    ...opts,
  });
  return res as {
    code: number;
    message?: string;
    success?: boolean;
    data?: any;
    [key: string]: any;
  };
};
