generated from template/astro-template
	
		
			
				
	
	
		
			31 lines
		
	
	
		
			872 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			31 lines
		
	
	
		
			872 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import { uploadChunkV2 } from '@/apps/draw/modules/upload';
 | 
						|
import { toFile } from '@/apps/draw/modules/to-file';
 | 
						|
import { toast } from 'react-toastify';
 | 
						|
 | 
						|
type UploadFileOpts = {
 | 
						|
  appKey: string;
 | 
						|
  version: string;
 | 
						|
  filename: string;
 | 
						|
  text: string;
 | 
						|
};
 | 
						|
const getFilenameExtension = (filename: string) => {
 | 
						|
  return filename.split('.').pop() || '';
 | 
						|
};
 | 
						|
const allowFilesName = ['js', 'css', 'json', 'html'];
 | 
						|
export const uploadFile = async (uploadFileOpts: UploadFileOpts) => {
 | 
						|
  const { appKey, version, filename, text } = uploadFileOpts;
 | 
						|
  const extension = getFilenameExtension(filename);
 | 
						|
  if (!allowFilesName.includes(extension)) {
 | 
						|
    toast.error('文件类型不支持');
 | 
						|
    return;
 | 
						|
  }
 | 
						|
  const file = toFile(text, filename);
 | 
						|
  const res = await uploadChunkV2(file, {
 | 
						|
    appKey,
 | 
						|
    version,
 | 
						|
    filename,
 | 
						|
    noCheckAppFiles: false,
 | 
						|
  });
 | 
						|
  return res as any;
 | 
						|
};
 |