Push de fichier avec API
-
Bonjour @Yannick,
Nous avons réalisé un certain nombres de mises à jour dans les API GoFAST qui n'ont pas encore été reportés en community.
Pourriez-vous me transmettre le contenu de votre fichier '/var/www/d7/sites/all/modules/gofast/gofast.module' et plus particulièrement la fonction 'gofast_api_post_node_node()'.
Si vous ne savez pas comment faire n'hésitez pas à revenir vers moi, nous prendrons contact avec vous pour vous assister à distance.
Merci d'avance et bonne journée !
Cordialement,
-
Bonjour et merci de votre retour.
Voici le code :function gofast_api_post_node_node($params){ //Check content type if(strpos($params['Content-Type'], "multipart/form-data") === FALSE){ //return gofast_api_not_implemented("This Content-Type"); } //Check authentication global $user; if($user->uid == 0){ gofast_api_not_authorized("Unavailable ressource for anonymous user"); } //Get params and check mandatory params $type = file_get_contents($_FILES['type']['tmp_name']); if(empty($type)){ if(!isset(json_decode(file_get_contents('php://input'), true)['type'])){ gofast_api_mandatory_parameter("type"); }else{ $type = json_decode(file_get_contents('php://input'), true)['type']; } } $title = file_get_contents($_FILES['title']['tmp_name']); if(empty($title)){ if(!isset(json_decode(file_get_contents('php://input'), true)['title'])){ gofast_api_mandatory_parameter("title"); }else{ $title = json_decode(file_get_contents('php://input'), true)['title']; } } if($type == "alfresco_item"){ //We should have locations param $locations = file_get_contents($_FILES['locations']['tmp_name']); if(empty($locations)){ if(!isset(json_decode(file_get_contents('php://input'), true)['locations'])){ gofast_api_mandatory_parameter("locations"); }else{ $locations = json_decode(file_get_contents('php://input'), true)['locations']; } } //We should have file param OR template_gid param $file = file_get_contents($_FILES['file']['tmp_name']); $template_nid = file_get_contents($_FILES['template_nid']['tmp_name']); if(empty($template_nid)){ if(!isset(json_decode(file_get_contents('php://input'), true)['template_nid'])){ gofast_api_mandatory_parameter("template_nid"); }else{ $template_nid = json_decode(file_get_contents('php://input'), true)['template_nid']; } } if(empty($file) && empty($template_nid)){ gofast_api_mandatory_parameter("file or template_nid"); } if(empty($file)){ //No file provided, we need to get the wanted template $node_template = node_load($template_nid); //check if the param indicating to keep the template_tags is true or not $keep_tags = file_get_contents($_FILES['keep_tags']['tmp_name']); if(empty($keep_tags)){ $keep_tags = json_decode(file_get_contents('php://input'), true)['keep_tags']; } //Check if we retrieved a node if($node_template == null){ gofast_api_internal_server_error("Unable to load node with nid : " . $template_nid); } //Check node access if(!node_access('view', $node_template)){ gofast_api_forbidden("You don't have the permission to view this node"); } if($node_template->type !== "alfresco_item"){ gofast_api_internal_server_error("Unable to retrieve associated content for given nid : " . $template_nid); } $file = gofast_cmis_item_get_content($node_template->field_reference[LANGUAGE_NONE][0]['value']); if(empty($file)){ //Check if we retrieved the content gofast_api_internal_server_error("Alfresco content not found for this node (Internal error)"); } } //Parse locations if needed if(is_string($locations)){ $locations = json_decode($locations, TRUE); } if(!is_array($locations) || count($locations) < 1){ gofast_api_internal_server_error("Unable to parse given locations"); } //Set our main location $main_location = reset($locations); //Now, we need to get spaces from these locations (and check the access) $spaces = array(); foreach($locations as $location){ //Try to get each gid corresponding to each location $gid = gofast_cmis_get_spaceid_by_webdav_folder($location, null); if($gid == NULL){ //If we can't retrieve a gid, it probably means there is no such folder gofast_api_internal_server_error("Unable to find " . $location); } if(!og_user_access("node", $gid, "create alfresco_item content")){ gofast_api_forbidden("Forbidden write permission to " . $location); } $spaces[$gid] = TRUE; } $spaces = array_keys($spaces); //Try to find file mimetype $extension = array_pop(explode('.', $title)); $mimetype = gofast_cmis_get_mimetype_extension_match_array()[$extension]; //Upload the file to it's main location $main_folder_item = gofast_cmis_item_get_by_path($main_location); $inserted_item = gofast_cmis_item_content_insert($main_folder_item->id, $title, $file, $mimetype); $node_ref = $inserted_item->properties['alfcmis:nodeRef']; if (!isset($inserted_item->id)) { gofast_api_internal_server_error("Document upload has failed. Maybe a document with the same title exists in the same folder."); } //Inform replication system that we handle the node creation variable_set("cmis_creating_" . $node_ref, "true"); //Document uploaded, create node programmatically $person = $user->name; $name = $title; $language = "und"; // !!!!!! $emplacement = $main_location; $node = gofast_cmis_create_node_programmatically($node_ref, $person, $name, false, $language, false, $gid, $emplacement, ""); if($keep_tags == 'true'){ //exclude Template tags if(count($node_template->field_tags) > 0){ foreach($node_template->field_tags[LANGUAGE_NONE] as $key=>$array_term){ $term = taxonomy_term_load($array_term["tid"]); if($term->name == "Template" || $term->name == "template"){ unset($node_template->field_tags[LANGUAGE_NONE][$key]); } } } $node->field_tags = $node_template->field_tags; $document_node = gofast_prepare_node_save_technically($node); $ruid = $document_node->revision_uid; node_save($document_node); db_update('node_revision') ->fields(array('uid' => $ruid)) ->condition('nid', $document_node->nid) ->execute(); } if(empty($node->nid)){ gofast_api_internal_server_error("Node creation has failed"); } //Multifile the document if needed if(count($locations > 1)){ $_POST['technically_save'] = TRUE; $_POST['pk'] = $node->nid; $_POST['name'] = GOFAST_CMIS_LOCATIONS_FIELD; $_POST['value'] = $locations; $_POST['non_xeditable'] = TRUE; gofast_update_node_field(); } //Success, return the node id gofast_api_success(array('nid' => $node->nid)); }else if(in_array($type, array('article', 'forum'))){ //We should have gids and body param $gids = file_get_contents($_FILES['gids']['tmp_name']); if(empty($gids)){ gofast_api_mandatory_parameter("gids"); } $body = file_get_contents($_FILES['body']['tmp_name']); if(empty($body)){ gofast_api_mandatory_parameter("body"); } //Parse gids if needed if(is_string($gids)){ $gids = json_decode($gids, TRUE); } //Check each space (existence and rights foreach($gids as $gid){ $space = node_load($gid); if($space == null ||! in_array($space->type, array("group", "organisation", "public", "extranet", "private_space"))){ gofast_api_internal_server_error("Unable to find a space corresponding to given id: " . $gid); } if(!og_user_access("node", $gid, "create alfresco_item content")){ gofast_api_forbidden("Forbidden write permission to " . $space->title . "/" . $gid); } } //Create the content module_load_include('inc', 'node', 'node.pages'); global $user; $node = new stdClass(); $node->title = $title; $node->type = $type; $node->language = $user->language; $node->options = array(); $node->promote = NODE_NOT_PROMOTED; $node->module = 'node'; $node->comment = COMMENT_NODE_OPEN; $node->status = NODE_PUBLISHED; $node->uid = $user->uid; if(in_array($type, array('forum'))){ $node->taxonomy_forums[LANGUAGE_NONE][0]['tid'] = reset(taxonomy_get_term_by_name("General discussion", 'forums'))->vid; } $node->body[LANGUAGE_NONE][0]['value'] = $body; $node->body[LANGUAGE_NONE][0]['format'] = "full_html"; node_save($node); foreach($gids as $gid){ gofast_og_subscribe_node($gid, $node->nid); } //Ised to fire the rule during node creation //see JIRA 2775 $node = node_load($node->nid, NULL, TRUE); rules_invoke_event('node_insert',$node); node_access_acquire_grants($node); gofast_api_success(array('nid' => $node->nid)); }else{ gofast_api_not_implemented("This type"); } }
Voici encore quelques précisions : j'attaque l'API avec du python 3.7 et nous sommes en version gofast 3.7.
Merci encore.
Yannick -
Merci pour votre retour !
Effectivement cette fonction a été modifiée et peut expliquer le comportement que vous avez, pourriez-vous essayer cette version :
function gofast_api_post_node_node($params){ //Check content type if(strpos($params['Content-Type'], "multipart/form-data") === FALSE){ //return gofast_api_not_implemented("This Content-Type"); } //Check authentication global $user; if($user->uid == 0){ gofast_api_not_authorized("Unavailable ressource for anonymous user"); } //Get params and check mandatory params $type = file_get_contents($_FILES['type']['tmp_name']); if(empty($type)){ if(isset(json_decode(file_get_contents('php://input'), true)['type'])){ $type = json_decode(file_get_contents('php://input'), true)['type']; }else if(isset($params['type'])){ $type = $params['type']; }else{ gofast_api_mandatory_parameter("type"); } } $title = file_get_contents($_FILES['title']['tmp_name']); if(empty($title) && $type != "contact"){ if(isset(json_decode(file_get_contents('php://input'), true)['title'])){ $title = json_decode(file_get_contents('php://input'), true)['title']; }else if(isset($params['title'])){ $title = $params['title']; }else{ gofast_api_mandatory_parameter("title"); } } if($type == "alfresco_item"){ //We should have locations param $locations = file_get_contents($_FILES['locations']['tmp_name']); if(empty($locations)){ if(isset(json_decode(file_get_contents('php://input'), true)['locations'])){ $locations = json_decode(file_get_contents('php://input'), true)['locations']; }else if(isset($params['locations'])){ $locations = json_decode($params['locations']); }else{ gofast_api_mandatory_parameter("locations"); } } //We should have file param OR template_gid param $file = file_get_contents($_FILES['file']['tmp_name']); //If we don't have a file in the 'file' field, try to get it from another one if(empty($file) && count($_FILES > 0)){ $file = file_get_contents(reset($_FILES)['tmp_name']); } $template_nid = file_get_contents($_FILES['template_nid']['tmp_name']); if(empty($template_nid)){ if(!isset(json_decode(file_get_contents('php://input'), true)['template_nid'])){ }else{ $template_nid = json_decode(file_get_contents('php://input'), true)['template_nid']; } } if(empty($file) && empty($template_nid)){ gofast_api_mandatory_parameter("file or template_nid"); } if(empty($file)){ //No file provided, we need to get the wanted template $node_template = node_load($template_nid); //check if the param indicating to keep the template_tags is true or not $keep_tags = file_get_contents($_FILES['keep_tags']['tmp_name']); if(empty($keep_tags)){ $keep_tags = json_decode(file_get_contents('php://input'), true)['keep_tags']; } //Check if we retrieved a node if($node_template == null){ gofast_api_internal_server_error("Unable to load node with nid : " . $template_nid); } //Check node access if(!node_access('view', $node_template)){ gofast_api_forbidden("You don't have the permission to view this node"); } if($node_template->type !== "alfresco_item"){ gofast_api_internal_server_error("Unable to retrieve associated content for given nid : " . $template_nid); } $file = gofast_cmis_item_get_content($node_template->field_reference[LANGUAGE_NONE][0]['value']); if(empty($file)){ //Check if we retrieved the content gofast_api_internal_server_error("Alfresco content not found for this node (Internal error)"); } } //Parse locations if needed if(is_string($locations)){ $locations = json_decode($locations, TRUE); } if(!is_array($locations) || count($locations) < 1){ gofast_api_internal_server_error("Unable to parse given locations"); } //Set our main location $main_location = reset($locations); //Now, we need to get spaces from these locations (and check the access) $spaces = array(); foreach($locations as $location){ //Try to get each gid corresponding to each location $gid = gofast_cmis_get_spaceid_by_webdav_folder($location, null); if($gid == NULL){ //If we can't retrieve a gid, it probably means there is no such folder gofast_api_internal_server_error("Unable to find " . $location); } if(!og_user_access("node", $gid, "create alfresco_item content")){ gofast_api_forbidden("Forbidden write permission to " . $location); } $spaces[$gid] = TRUE; } $spaces = array_keys($spaces); //Try to find file mimetype $extension = array_pop(explode('.', $title)); $mimetype = gofast_cmis_get_mimetype_extension_match_array()[$extension]; //Check if this document already exists $existing_document = gofast_cmis_item_get_by_path($main_location . "/" . $title); if(!empty($existing_document->properties['gofast:nid'])){ $params['nid'] = $existing_document->properties['gofast:nid']; gofast_api_node_content($params); } //Upload the file to it's main location $main_folder_item = gofast_cmis_item_get_by_path($main_location); $inserted_item = gofast_cmis_item_content_insert($main_folder_item->id, $title, $file, $mimetype); $node_ref = $inserted_item->properties['alfcmis:nodeRef']; if (!isset($inserted_item->id)) { gofast_api_internal_server_error("Document upload has failed. Maybe a document with the same title exists in the same folder."); } //Inform replication system that we handle the node creation variable_set("cmis_creating_" . $node_ref, "true"); //Document uploaded, create node programmatically $person = $user->name; $name = $title; $language = "und"; // !!!!!! $emplacement = $main_location; $node = gofast_cmis_create_node_programmatically($node_ref, $person, $name, false, $language, false, $gid, $emplacement, ""); if($keep_tags == 'true'){ //exclude Template tags if(count($node_template->field_tags) > 0){ foreach($node_template->field_tags[LANGUAGE_NONE] as $key=>$array_term){ $term = taxonomy_term_load($array_term["tid"]); if($term->name == "Template" || $term->name == "template"){ unset($node_template->field_tags[LANGUAGE_NONE][$key]); } } } $node->field_tags = $node_template->field_tags; $document_node = gofast_prepare_node_save_technically($node); $ruid = $document_node->revision_uid; node_save($document_node); db_update('node_revision') ->fields(array('uid' => $ruid)) ->condition('nid', $document_node->nid) ->execute(); } if(empty($node->nid)){ gofast_api_internal_server_error("Node creation has failed"); } //Multifile the document if needed if(count($locations > 1)){ $_POST['technically_save'] = TRUE; $_POST['pk'] = $node->nid; $_POST['name'] = GOFAST_CMIS_LOCATIONS_FIELD; $_POST['value'] = $locations; $_POST['non_xeditable'] = TRUE; gofast_update_node_field(); } //Success, return the node id gofast_api_success(array('nid' => $node->nid, 'link' => '/node/' . $node->nid, "date" => date("c"))); }else if(in_array($type, array('article', 'forum'))){ //We should have gids and body param $gids = file_get_contents($_FILES['gids']['tmp_name']); if(empty($gids)){ gofast_api_mandatory_parameter("gids"); } $body = file_get_contents($_FILES['body']['tmp_name']); if(empty($body)){ gofast_api_mandatory_parameter("body"); } //Parse gids if needed if(is_string($gids)){ $gids = json_decode($gids, TRUE); } //Check each space (existence and rights foreach($gids as $gid){ $space = node_load($gid); if($space == null ||! in_array($space->type, array("group", "organisation", "public", "extranet", "private_space"))){ gofast_api_internal_server_error("Unable to find a space corresponding to given id: " . $gid); } if(!og_user_access("node", $gid, "create alfresco_item content")){ gofast_api_forbidden("Forbidden write permission to " . $space->title . "/" . $gid); } } //Create the content module_load_include('inc', 'node', 'node.pages'); global $user; $node = new stdClass(); $node->title = $title; $node->type = $type; $node->language = $user->language; $node->options = array(); $node->promote = NODE_NOT_PROMOTED; $node->module = 'node'; $node->comment = COMMENT_NODE_OPEN; $node->status = NODE_PUBLISHED; $node->uid = $user->uid; if(in_array($type, array('forum'))){ $node->taxonomy_forums[LANGUAGE_NONE][0]['tid'] = reset(taxonomy_get_term_by_name("General discussion", 'forums'))->vid; } $node->body[LANGUAGE_NONE][0]['value'] = $body; $node->body[LANGUAGE_NONE][0]['format'] = "full_html"; node_save($node); foreach($gids as $gid){ gofast_og_subscribe_node($gid, $node->nid); } //Ised to fire the rule during node creation //see JIRA 2775 $node = node_load($node->nid, NULL, TRUE); rules_invoke_event('node_insert',$node); node_access_acquire_grants($node); gofast_api_success(array('nid' => $node->nid)); }else { gofast_api_not_implemented("This type"); } }
Merci d'avance !
Cordialement,
-
Bonjour,
Je viens d'essayer cette version et effectivement celà fonctionne, mais j'ai toujours un manque au niveau du file, il me dit : 'file or template_nid is a mandatory parameter'. Je passe le fichier comme octet-stream, est-ce la bonne méthode ?
Merci encore pour votre réactivité.
Yannick -
@Yannick said in Push de fichier avec API:
requests.post(url, params=data, headers=headers)
Bonsoir @Yannick,
D'après la documentation python, pour attacher un fichier il conviendrait plutôt de le passer directement dans le requests.post de cette manière :
requests.post(url, params=data, headers=headers) url = "https://gofast.ceo-vision.com/api/node/node" headers = {'Content-Type': "application/json", 'Authorization': "Basic user:password(Base 64 encoded)"} data = {'type': 'alfresco_item', 'title': file.filename,'locations':'/Site/_Organisation/myorga/2021/'} files = {'file': file} print(requests.post(url, params=data, headers=headers, files=files).json()
Bonne soirée !
Cordialement,
-
Bonjour, j'ai également un problème pour push un fichier avec l'API (GoFast version 4).
Je ne comprends pas très bien la documentation, qui donne deux headers différents et deux body différents.
Pour l'instant, j'ai cette requête:POST /api/node/node HTTP/1.1 Authorization: Basic login:mdp en base64 Host: gofast.univ-lemans.fr Content-Type: application/json {"type": "alfresco_item", "locations": "/Sites/_vgirod#ogdocuments", "file": "test", "title":"titre"}
qui me renvoie "file or template_nid is a mandatory parameter"
J'ai l'impression que je dois faire deux requêtes à la suite? Et si oui, avez-vous un exemple fonctionnel?
-
Bonjour @vgirod
Quel outil ou language utilisez vous pour communiquer avec l'API ?
La manière de poster un fichier diffère pas mal selon le language qu'on utilise (Python, PHP, JS etc)
Merci d'avance !
Cordialement,
-
J'utilise java mais ici, j'ai utilisé un testeur d'api lambda en ligne pour voir si justement le problème vennait de mon code.
La requête une fois envoyée est la même quelque soit le language utilisé il me semble, mais oui, le code change beaucoup en fonction du language. J'utilise ce code pour le moment:
(en JAVA)URL url = new URL("https://gofast.univ-lemans.fr/api/node/node"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); String basicAuth = Base64.getEncoder().encodeToString("login:mdp".getBytes()); conn.setRequestProperty ("Authorization", "Basic "+basicAuth); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestMethod("POST"); conn.setDoOutput(true); OutputStream osw = conn.getOutputStream(); String body = "{\"type\": \"alfresco_item\"," + "\"locations\": \"/Sites/_vgirod#ogdocuments\"," + "\"file\": \"test\"," + "\"title\":\"titre\"}"; osw.write(body.getBytes()); osw.flush(); osw.close(); conn.connect();
-
Oui je vois, le Content-Type global doit être de type multipart/form-data. Ce Content type permet de transmettre des parties "fichiers" et des parties "formulaire", ce qui correspond à notre JSON.
Si j'utilise advanced rest client, cela correspond à :
Et dans le body j'ai mon fichier :
Puis mes données :
Attention d'ailleurs, votre champ locations doit être un tableau en json format texte (exemple : ["/Sites/_jlemangarin"]) sans le #ogdocuments.
J'ai trouvé des exemples d'utilisation ici mais je n'ai jamais essayé de le faire en Java, si vous n'y arrivez pas j'essaierai de le faire dans la matinée. J'ai peut être un exemple d'utilisation quelque part dans un des modules Bonita que nous utilisons.
-
-
Le code fourni sur cette page fonctionne parfaitement pour du java:
https://blog.cpming.top/p/httpurlconnection-multipart-form-data
-
Super, merci pour ce retour !