Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

33
LINES

< > BotCompany Repo | #1015418 // uploadFileToFileServer - returns URL or fails

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (11426L/68K).

static S uploadFileToFileServer(S pathOrURL) {
  File f = urlToFileOpt(pathOrURL);
  if (f != null)
    ret uploadFileToFileServer(f);
  else if (isURL(pathOrURL)) {
    File tmp = createTempFile();
    afterwards { deleteFile(tmp); }
    loadBinaryPageToFile(pathOrURL, tmp);
    ret uploadFileToFileServer(tmp, pathOrURL);
  } else
    ret uploadFileToFileServer(newFile(pathOrURL));
}

static S uploadFileToFileServer(File f) {
  ret uploadFileToFileServer(f, f.getName());
}

// TODO: optimize
static S uploadFileToFileServer(File f, S name) {
  assertFileExists(f);
  ret uploadFileToFileServer(loadBinaryFile(f), name);
}

static S uploadFileToFileServer(byte[] data, S name) {
  print("Uploading " + l(data) + " bytes (" + quote(name) + ") to file server.");
  S page = postPage("https://botcompany.de/files/upload", "data", bytesToHex(data), "name", name, '_pass, muricaPassword());
  print(page);
  Matcher m = Pattern.compile("/raw/([0-9]+)").matcher(page);
  if (!m.find()) fail("File server said: " + page);
  S url = "https://botcompany.de/files/" + m.group(1);
  infoBox("Uploaded: " + url);
  ret url;
}

Author comment

Began life as a copy of #1010476

download  show line numbers  debug dex  old transpilations   

Travelled to 19 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, omdjrrnzbjjv, podlckwnjdmb, pyentgdyhuwx, pzhvpgtvlbxg, snaazhdonpnp, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1015418
Snippet name: uploadFileToFileServer - returns URL or fails
Eternal ID of this version: #1015418/17
Text MD5: a25f32dfcf0c57d960e4cc4d5119903b
Transpilation MD5: bc39c8d83a7e419afdb4affb85c4f689
Author: stefan
Category: javax / networking
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-28 15:14:19
Source code size: 1150 bytes / 33 lines
Pitched / IR pitched: No / No
Views / Downloads: 361 / 631
Version history: 16 change(s)
Referenced in: [show references]