Download file with apache chemistry php client

573 Views Asked by At

I need to know what I've to do if I want to download a cmis:documento that I've in my repositorio with Apache Chemistry Php client.

I've one file, "Test.txt" in Sitios space... I want download Test.txt with browser...

My code:

$client = new CMISService('http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom', 'admin', 'admin');
$path = '/Sitios';
$directorio = $client->getObjectByPath($path);

$objs = $client->getChildren($directorio->id);
foreach ($objs->objectList as $obj)
{
    var_dump ($obj);
}

In $obj I've:

 array (size=32)
      'alfcmis:nodeRef' => string 'workspace://SpacesStore/c3c903fe-86b6-4db0-8cb2-d5c5dbe913c7' (length=60)
      'cmis:isImmutable' => string 'false' (length=5)
      'cmis:versionLabel' => string '1.0' (length=3)
      'cmis:objectTypeId' => string 'cmis:document' (length=13)
      'cmis:description' => string 'descripcion del fichero' (length=23)
      'cmis:createdBy' => string 'admin' (length=5)
      'cmis:checkinComment' => null
      'cmis:creationDate' => string '2016-02-15T01:25:12.76+01:00' (length=28)
      'cmis:isMajorVersion' => string 'true' (length=4)
      'cmis:contentStreamFileName' => string 'Fichero de texto plano' (length=22)
      'cmis:name' => string 'Fichero de texto plano' (length=22)
      'cmis:isLatestVersion' => string 'true' (length=4)
      'cmis:lastModificationDate' => string '2016-02-15T01:25:12.76+01:00' (length=28)
      'cmis:contentStreamLength' => string '21' (length=2)
      'cmis:objectId' => string 'c3c903fe-86b6-4db0-8cb2-d5c5dbe913c7;1.0' (length=40)
      'cmis:lastModifiedBy' => string 'admin' (length=5)
      'cmis:secondaryObjectTypeIds' => string 'P:rn:renditioned' (length=16)
      'cmis:contentStreamId' => string 'store://2016/2/15/1/25/c36a749d-43f1-4e31-b46a-de66b4f5634d.bin' (length=63)
      'cmis:contentStreamMimeType' => string 'text/plain' (length=10)
      'cmis:baseTypeId' => string 'cmis:document' (length=13)
      'cmis:changeToken' => null
      'cmis:isPrivateWorkingCopy' => string 'false' (length=5)
      'cmis:versionSeriesCheckedOutBy' => null
      'cmis:isVersionSeriesCheckedOut' => string 'false' (length=5)
      'cmis:versionSeriesId' => string 'c3c903fe-86b6-4db0-8cb2-d5c5dbe913c7' (length=36)
      'cmis:isLatestMajorVersion' => string 'true' (length=4)
      'cmis:versionSeriesCheckedOutId' => null
      'cm:title' => string 'Titulo del fichero' (length=18)
      'cm:description' => string 'descripcion del fichero' (length=23)
      'app:editInline' => string 'true' (length=4)
      'cm:lastThumbnailModification' => string 'pdf:1455495914744' (length=17)
      '' => string 'Titulo del fichero' (length=18)
  public 'renditions' => 

What I've to do to create a link to download the file? I don't want to use webscript...

I think that I've to use getContentStream($obj->id) ¿really? Thanks.

1

There are 1 best solutions below

0
On

Depending on your document mime type and what you are willing to do with, it can be slightly different, but basically this is what you need to do, if you were using java:

  // Get the contents of the file
    Document doc = (Document) session.getObject(id);
    ContentStream contentStream = doc.getContentStream(); // returns null if the document has no content
    if (contentStream != null) {
        String content = getContentAsString(contentStream);
        System.out.println("Contents of " + filename + " are: " + content);
    } else {
        System.out.println("No content.");
    }

    ...

    /**
     * Helper method to get the contents of a stream
     * 
     * @param stream
     * @return
     * @throws IOException
     */
    private static String getContentAsString(ContentStream stream) throws IOException {
        StringBuilder sb = new StringBuilder();
        Reader reader = new InputStreamReader(stream.getStream(), "UTF-8");

        try {
            final char[] buffer = new char[4 * 1024];
            int b;
            while (true) {
                b = reader.read(buffer, 0, buffer.length);
                if (b > 0) {
                    sb.append(buffer, 0, b);
                } else if (b == -1) {
                    break;
                }
            }
        } finally {
            reader.close();
        }

        return sb.toString();
    }

but since you are on PHP, I guess you should be replicating the same logic with php api fuctions, so basically $client->getContentStream($objId); should return the file content as string.