hkc_files_management.anubis 3.37 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 20/08/2019
 * Time: 16:47
 * © David RENÉ
 */

read system/files.anubis
read xlib/web/controllers_web_site.anubis
read database/generated/fs_file.anubis
read xlib/web_controllers/language/language_management.anubis
read xlib/web_controllers/fs/fs_core.anubis
read xlib/web/json.anubis

define DB_id
  get_new_hkc_id_from_fs
  (
    SQLite3DataBase db
  )
  =
  if get_fs_file_by_clause(db, "res_model = 'hkc' ORDER BY res_id DESC") is
  {
    failure           then db_id(0),
    success(fs_file)  then db_id(fs_file.res_id + 1)
  }
.

public define (WEB_Session) -> WEB_Controller_Result
  hkc_upload
  (
    SQLite3DataBase db
  )=
  (
    WEB_Session _session
  )|->
  with             _T = make_translate_function(_session),
                  lwa = *_session.web_request.lwa,
  if file_upload_value(lwa,"upload") is 
  {
    failure       then    
      //set_Page_Message(_session.fields, error_translate("HK_UPLOAD_FILE_NAME_NOT_FOUND"));
      ajax(json(http_ok, json_object([json_member("uploaded", json_int(0)),
                                      json_member("error", json_object([json_member("message", json_string("File not found"))]))]))),
    success(upl_file) then
      //uploaded filename and temporary location of this file
      since upl_file is (filename, temp_filepath),
      println("filename="+filename+" / temp_filepath="+temp_filepath);
      with hkc_file_id  = get_new_hkc_id_from_fs(db),
      with root_dir = app_fs+"hkc/"+to_fs_folder(hkc_file_id),
      make_directories(root_dir);
      
//      with end_dir        = current_dir + extract_dir(upload_full_path),
      with final_filename = root_dir + filename,
      
      if move_file(temp_filepath, final_filename) then
        //if the file is image create thumbnail
//        since get_MIME_from_filename(final_filename) is mime(mime_type, subtype, _),
//        (if mime_type = "image" then
//          create_thumbnail(root_dir + end_dir, filename, "64x64");
//          create_thumbnail(root_dir + end_dir, filename, "128x128")
//        else
//          unique);
        set_file(db, "hkc", hkc_file_id, filename, "file", filename, "hkc/"+to_fs_folder(hkc_file_id));
        ajax(json(http_ok, json_object([json_member("uploaded", json_int(1)),
                                        json_member("fileName", json_string(filename)),
                                        json_member("url", json_string("?aws_controller=hk_c&aws_action=download&file="+hkc_file_id))])))
      else
        println("move_file error from ["+temp_filepath+"] to ["+final_filename+"]");
      ajax(json(http_ok, json_object([json_member("uploaded", json_int(0)),
                                      json_member("error", json_object([json_member("message", json_string("move_file error from temporary location"))]))]))),

  }
.

public define (WEB_Session) -> WEB_Controller_Result    
  hkc_download
  (
    SQLite3DataBase db,
  )=
  (
    WEB_Session _session
  ) |->
  with     _T = make_translate_function(_session),
  web_site_public_dir = get_String(_session.fields, "AWS_WEB_SITE_PUBLIC_DIR", ""),
          lwa = *_session.web_request.lwa,
     file_id = get_DB_id(lwa, "file"),

  if get_file(db, "hkc", file_id, "file") is
  {
    failure       then  send_file(web_site_public_dir+"img/missing-file.png", inline),
    success(file) then  send_file(file, inline)
  }
.