使用FileDoge的API保存文件

说明

通过FileDoge的API上传文件,并将上传文件后返回的文件信息保存到本地文件中,该文件保留在家目录下。

运行脚本,如果要上传文件到云端,第一个选项应为"-upload",则第二个选项为文件路径,上传文件后会返回文件的"token"和"delete_token"等基本信息,并且会保存上传时产生的日志文件在本地,上传结束后会返回日志文件所在路径;如果要查看云端文件的信息,则第一个选项应为"-file",第二个选项应为对应云端的"token";如果要删除云端文件,则第一个选项应为"-delete",第二个选项为"token",第三个选项为"delete_token";如果要下载文件,则第一个选项应为"-download",第二个选项为"上传结束后会返回日志文件所在路径",脚本会从日志文件中检测到"token"并将相应的文件下载下来,如果是大于200MB的文件则会自动合并;如果要查看帮助手册,则第一个选项应为"–help"。

脚本下载

点击下载:filedoge.sh

下载脚本后记得给脚本执行权限

1
  chmod +x filedoge.sh

代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  cmdself=$0

  if [ -z "$1" ]; then
	  echo "Upload File: ${cmdself} -upload \"filepath\""
	  echo "File Information: ${cmdself} -file \"token\""
	  echo "Delete File: ${cmdself} -delete \"token\" \"delete_token\""
	  echo "Download File: ${cmdself} -download \"Upload file log\""
	  echo "Help: ${cmdself} --help"
  fi  

  # Upload File
  if [ "$1" == "-upload" ]; then	  
	  filePath=$2
	  filePathName=$(echo "${filePath}" | tr '\/' ' ' | awk '{print $NF}')
	  FileDogeLogDir="${HOME}/FileDoge/Log"	  
	  log=${FileDogeLogDir}/${filePathName}/${filePathName}.txt.1
	  if [ ! -d "${FileDogeLogDir}/${filePathName}" ]; then
		  mkdir -p "${FileDogeLogDir}/${filePathName}"
	  fi	  
	  compile_size=$((200 * 1024 * 1024))
	  compile_file_size=$(stat -c%s "${filePath}")
	  if [ "${compile_file_size}" -ge "${compile_size}" ]; then
		  split -d -b 199m ${filePath} ${filePath}_split_
		  lookup_var=$(ls -l ${filePath}_split_* | sort -nr | nl | tail -n 1 | awk '{print $1}')
		  if [ -f "${log}" ]; then
			  if [ "${filePathName}_*.txt" ]; then
				  flag=0
				  flag=$(ls "${FileDogeLogDir}/${filePathName}/${filePathName}".txt.* | tail -n 1 | awk -F '.' '{print $NF}')
				  log=${FileDogeLogDir}/${filePathName}/${filePathName}.txt.$((flag + 1))
			  fi
		  fi
		  for ((i = 0; i < ${lookup_var}; i++))
		  do
			  lookup_num=$(printf "%02d\n" $i)
			  curl --location --request POST 'https://api.filedoge.com/upload' -H "Content-Type: multipart/form-data;" --form "file=@${filePath}_split_${lookup_num}" | cat >> ${log} && echo "$(readlink -f ${log})" && cat ${log}
			  echo "" >> ${log}
		  done
	  else
		  if [ -f "${log}" ]; then
			  flag=0
			  flag=$(ls "${FileDogeLogDir}/${filePathName}/${filePathName}".txt.* | tail -n 1 | awk -F '.' '{print $NF}')
			  log=${FileDogeLogDir}/${filePathName}/${filePathName}.txt.$((flag + 1))
		  fi
		  curl --location --request POST 'https://api.filedoge.com/upload' -H "Content-Type: multipart/form-data;" --form "file=@${filePath}" | cat >> ${log} && echo "$(readlink -f ${log})" && cat ${log}
		  echo "" >> ${log}
	  fi
  fi

  if [ "$1" == "-file" ] || [ "$1" == "-delete" ]; then
	  token=$2
	  # File Information
	  if [ "$1" == "-file" ]; then
		  curl --location --request GET "https://api.filedoge.com/info/${token}"
		  # Delete File
	  elif [ "$1" == "-delete" ]; then
		  delete_token=$3
		  curl --location --request POST "https://api.filedoge.com/delete/${token}/{$delete_token}"

	  fi
  fi
  # Download File
  if [ "$1" == "-download" ]; then
	  multifile_token=$2
	  multifile_token_num=$(cat ${multifile_token} | awk -F '"' '{print $4}' | nl | tail -n 1 | awk '{print $1}')
	  if [ "${multifile_token_num}" == 1 ]; then
		  token=$(cat ${multifile_token} | awk -F '"' '{print $4}')
		  download_file_name=$(${cmdself} -file ${token} | tr ',' ' ' | tr ' ' '\n' | grep --color "originalFilename" | tr '"' ' ' | awk '{print $3}')
		  download_file_url=$(${cmdself} -file ${token} | tr ',' ' ' | tr ' ' '\n' | grep --color "downloadUrl" | tr '"' ' ' | awk '{print $3}')
		  FileDogeDownloadDir="${HOME}/FileDoge/Download/${download_file_name}"
		  if [ ! -d "$FileDogeDownloadDir" ]; then
			  mkdir -p ${FileDogeDownloadDir}
		  fi
		  wget -P ${FileDogeDownloadDir} ${download_file_url}		
		  if [ -f "${FileDogeDownloadDir}/${download_file_name}" ]; then
			  latest_file=$(ls "${FileDogeDownloadDir}/${download_file_name}".* 2>/dev/null | tail -n 1)
			  if [ -n "${latest_file}" ]; then
				  flag=0
				  flag=$(ls "${FileDogeDownloadDir}/${download_file_name}".* | tail -n 1 | awk -F '.' '{print $NF}')
				  mv -v ${FileDogeDownloadDir}/${token} ${FileDogeDownloadDir}/${download_file_name}.$((flag + 1))
			  else
				  mv -v  ${FileDogeDownloadDir}/${token} ${FileDogeDownloadDir}/${download_file_name}.$((flag + 1))
			  fi
		  else
			  mv -v ${FileDogeDownloadDir}/${token} ${FileDogeDownloadDir}/${download_file_name}			  
		  fi
	  elif [ "${multifile_token_num}" -gt "1" ]; then
		  filePath=$2
		  catFileName=$(cat ${filePath} | sed -n "1"p | awk -F '"' '{print $14}' | awk -F '_' '{print $1}')
		  splitFileName=$(cat ${filePath} | sed -n "1"p | awk -F '"' '{print $14}' | tr -d '0-9')
		  for ((i = 1; i <= ${multifile_token_num}; i++))
		  do
			  token=$(cat ${multifile_token} | awk -F '"' '{print $4}' | nl | sed -n "${i}"p | awk '{print $2}')	  
			  download_file_name=$(${cmdself} -file ${token} | tr ',' ' ' | tr ' ' '\n' | grep --color "originalFilename" | tr '"' ' ' | awk '{print $3}')
			  download_file_url=$(${cmdself} -file ${token} | tr ',' ' ' | tr ' ' '\n' | grep --color "downloadUrl" | tr '"' ' ' | awk '{print $3}')
			  FileDogeDownloadDir="${HOME}/FileDoge/Download/${catFileName}"
			  if [ ! -d "$FileDogeDownloadDir" ]; then
				  mkdir -p ${FileDogeDownloadDir}
			  fi
			  wget -P ${FileDogeDownloadDir} ${download_file_url}			
			  if [ -f "${FileDogeDownloadDir}/${download_file_name}" ]; then
				  latest_file=$(ls "${FileDogeDownloadDir}/${download_file_name}".* 2>/dev/null | tail -n 1)
				  if [ -n "${latest_file}" ]; then
					  flag=0
					  flag=$(ls "${FileDogeDownloadDir}/${download_file_name}".* | tail -n 1 | awk -F '.' '{print $NF}')
					  mv -v ${FileDogeDownloadDir}/${token} ${FileDogeDownloadDir}/${download_file_name}.$((flag + 1))
				  else
					  mv -v ${FileDogeDownloadDir}/${token} ${FileDogeDownloadDir}/${download_file_name}.$((flag + 1))
				  fi
			  else
				  mv -v ${FileDogeDownloadDir}/${token} ${FileDogeDownloadDir}/${download_file_name}

			  fi
		  done
		  if [ -f "${FileDogeDownloadDir}/${catFileName}" ]; then			  
			  rm -rf "${FileDogeDownloadDir}/${catFileName}"
		  fi
		  for ((i = 0; i < multifile_token_num; i++))
		  do
			  lookup_num=$(printf "%02d\n" $i)
			  cat ${FileDogeDownloadDir}/${splitFileName}$(printf "%02d" $((lookup_num))) >> ${FileDogeDownloadDir}/${catFileName}
		  done
	  fi
  fi

  if [ "$1" == "--help" ] && [ -z "$2" ]; then
	  echo "File Doge API"
	  echo "File Doge offers a free and unmetered API to upload and generate download links for files up to 200mb in size, although we do not guarantee that the API remain free or unmetered in the future"
	  echo ""
	  echo ""
	  echo "Upload File"
	  echo "You will send us a file with POST request with a size less than 200mb"
	  echo "curl --location --request POST 'https://api.filedoge.com/upload' -H \"Content-Type: multipart/form-data;\" --form 'file=@filePath'"
	  echo "Successfull Response"
	  echo "{"
	  echo "	token: fileToken,"
	  echo "	"name": fileName,"
	  echo "	"size": fileSizeInBytes,"
	  echo "	"originalFilename": originalFilename,"
	  echo "	"createdAt": creationDate,"
	  echo "	"mimeType": fileMimeType"
	  echo "	"deleteToken": fileDeleteToken"
	  echo "}"
	  echo ""
	  echo ""
	  echo "File Information"
	  echo ""
	  echo "You will send us a file token with GET request to get information"
	  echo "curl --location --request GET 'https://api.filedoge.com/info/TOKEN'"
	  echo "Successfull Response"
	  echo "{"
	  echo "	token: fileToken,"
	  echo "	"name": fileName,"
	  echo "	"size": fileSizeInBytes,"
	  echo "	"originalFilename": originalFilename,"
	  echo "	"createdAt": creationDate,"
	  echo "	"mimeType": fileMimeType"
	  echo "	"downloadUrl": directUrlToDownloadFile"
	  echo "}"
	  echo ""
	  echo ""
	  echo "Delete File"
	  echo "You can delete your files using the file token and delete identifier"
	  echo "curl --location --request POST 'https://api.filedoge.com/delete/\$TOKEN/\$DELETE_TOKEN'"
	  echo "Successfull Response"
	  echo "{"
	  echo "	messgae: "File Deleted Successfully","
	  echo "}"
	  echo ""
	  echo ""
	  echo "Errors"
	  echo "If you or us encounter an error we respond with 200 OK and a JSON which is specify what's went wrong"
	  echo "Error Example"
	  echo "{"
	  echo "	error: errorMessage,"
	  echo "}"
  fi