Skip to content
  • Categories
  • Recent
  • Popular
Collapse
Brand Logo
  1. Home
  2. Categories
  3. Sujets Techniques
  4. Push de fichier avec API

Push de fichier avec API

Scheduled Pinned Locked Moved Sujets Techniques
14 Posts 3 Posters 596 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jlemangarinJ Offline
    jlemangarinJ Offline
    jlemangarin
    ADMIN SUPPORT-PROD DEV
    wrote on last edited by
    #4

    @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,

    LEMAN-GARIN Jérôme - CEO-Vision IT
    jerome.leman@ceo-vision.com
    Alliance - Porte A, 178 rue des Frères Lumière 74160 Archamps Technopole, FRANCE
    +33 (0) 811 693 111 et depuis l'étranger +33 (0) 972 236 057
    https://www.ceo-vision.com

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      Yannick
      wrote on last edited by
      #5

      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

      1 Reply Last reply
      0
      • jlemangarinJ Offline
        jlemangarinJ Offline
        jlemangarin
        ADMIN SUPPORT-PROD DEV
        wrote on last edited by
        #6

        @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,

        LEMAN-GARIN Jérôme - CEO-Vision IT
        jerome.leman@ceo-vision.com
        Alliance - Porte A, 178 rue des Frères Lumière 74160 Archamps Technopole, FRANCE
        +33 (0) 811 693 111 et depuis l'étranger +33 (0) 972 236 057
        https://www.ceo-vision.com

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vgirod
          wrote on last edited by
          #7

          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?

          Le Mans Université
          Direction des Systèmes Informatiques
          valentin.girod@univ-lemans.fr

          1 Reply Last reply
          0
          • jlemangarinJ Offline
            jlemangarinJ Offline
            jlemangarin
            ADMIN SUPPORT-PROD DEV
            wrote on last edited by
            #8

            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,

            LEMAN-GARIN Jérôme - CEO-Vision IT
            jerome.leman@ceo-vision.com
            Alliance - Porte A, 178 rue des Frères Lumière 74160 Archamps Technopole, FRANCE
            +33 (0) 811 693 111 et depuis l'étranger +33 (0) 972 236 057
            https://www.ceo-vision.com

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vgirod
              wrote on last edited by vgirod
              #9

              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();
              

              Le Mans Université
              Direction des Systèmes Informatiques
              valentin.girod@univ-lemans.fr

              1 Reply Last reply
              0
              • jlemangarinJ Offline
                jlemangarinJ Offline
                jlemangarin
                ADMIN SUPPORT-PROD DEV
                wrote on last edited by
                #10

                @vgirod

                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 à :

                15608ddc-d254-4f9e-906a-b08d35945d46-image.png

                Et dans le body j'ai mon fichier :

                caba980e-f385-4417-b55b-7c01311d8264-image.png

                Puis mes données :

                b4701459-feed-4613-a086-afe0cb16d4bd-image.png

                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.

                LEMAN-GARIN Jérôme - CEO-Vision IT
                jerome.leman@ceo-vision.com
                Alliance - Porte A, 178 rue des Frères Lumière 74160 Archamps Technopole, FRANCE
                +33 (0) 811 693 111 et depuis l'étranger +33 (0) 972 236 057
                https://www.ceo-vision.com

                1 Reply Last reply
                1
                • jlemangarinJ Offline
                  jlemangarinJ Offline
                  jlemangarin
                  ADMIN SUPPORT-PROD DEV
                  wrote on last edited by
                  #11

                  @vgirod

                  Avez-vous pu avancer sur le sujet ?

                  N'hésitez pas à revenir vers moi !

                  LEMAN-GARIN Jérôme - CEO-Vision IT
                  jerome.leman@ceo-vision.com
                  Alliance - Porte A, 178 rue des Frères Lumière 74160 Archamps Technopole, FRANCE
                  +33 (0) 811 693 111 et depuis l'étranger +33 (0) 972 236 057
                  https://www.ceo-vision.com

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    vgirod
                    wrote on last edited by
                    #12

                    Bonjour, j'ai réussi à faire une requête fonctionnelle avec le même logiciel que vous, votre exemple est parfait, je vais me pencher sur le code java cet après midi et poster ma solution quand j'en aurais une.

                    Le Mans Université
                    Direction des Systèmes Informatiques
                    valentin.girod@univ-lemans.fr

                    1 Reply Last reply
                    1
                    • V Offline
                      V Offline
                      vgirod
                      wrote on last edited by
                      #13

                      Le code fourni sur cette page fonctionne parfaitement pour du java:

                      https://blog.cpming.top/p/httpurlconnection-multipart-form-data

                      Le Mans Université
                      Direction des Systèmes Informatiques
                      valentin.girod@univ-lemans.fr

                      1 Reply Last reply
                      1
                      • jlemangarinJ Offline
                        jlemangarinJ Offline
                        jlemangarin
                        ADMIN SUPPORT-PROD DEV
                        wrote on last edited by
                        #14

                        @vgirod

                        Super, merci pour ce retour !

                        LEMAN-GARIN Jérôme - CEO-Vision IT
                        jerome.leman@ceo-vision.com
                        Alliance - Porte A, 178 rue des Frères Lumière 74160 Archamps Technopole, FRANCE
                        +33 (0) 811 693 111 et depuis l'étranger +33 (0) 972 236 057
                        https://www.ceo-vision.com

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Search
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Popular
                        • Search