2828import java .util .Collections ;
2929import java .util .HashMap ;
3030import java .util .Map ;
31+ import java .util .zip .GZIPInputStream ;
3132
3233/**
3334 * A Couchbase Lite Document Attachment.
@@ -124,15 +125,36 @@ public String getContentType() {
124125 public InputStream getContent () throws CouchbaseLiteException {
125126 if (body != null ) {
126127 return body ;
127- }
128- else {
128+ } else {
129129 Database db = revision .getDatabase ();
130- Attachment attachment = db .getAttachmentForSequence (revision .getSequence (), this .name );
130+ long sequence = getAttachmentSequence ();
131+ if (sequence == 0 ) {
132+ throw new CouchbaseLiteException (Status .INTERNAL_SERVER_ERROR );
133+ }
134+ Attachment attachment = db .getAttachmentForSequence (sequence , this .name );
131135 body = attachment .getContent ();
136+ if (attachment .getGZipped ()) {
137+ // Client does not expect a gzipped stream.
138+ // Only Router handles gzipped streams and uses getAttachmentForSequence directly.
139+ try {
140+ body = new GZIPInputStream (body );
141+ } catch (IOException e ) {
142+ throw new CouchbaseLiteException (e .getMessage (), Status .STATUS_ATTACHMENT_ERROR );
143+ }
144+ }
145+ gzipped = false ;
132146 return body ;
133147 }
134148 }
135149
150+ private long getAttachmentSequence () {
151+ long sequence = revision .getSequence ();
152+ if (sequence == 0 ) {
153+ sequence = revision .getParentSequence ();
154+ }
155+ return sequence ;
156+ }
157+
136158 /**
137159 * This is just for compatibility with iOS implementation.
138160 *
@@ -141,16 +163,16 @@ public InputStream getContent() throws CouchbaseLiteException {
141163 @ InterfaceAudience .Private
142164 public URL getContentURL (){
143165 try {
144- if (revision .getSequence () > 0 ) {
145- //Database db = revision.getDatabase();
146- //Attachment attachment = db.getAttachmentForSequence(revision.getSequence(), this.name);
147- String path = revision .getDatabase ().getAttachmentPathForSequence (revision .getSequence (), this .name );
166+ long sequence = getAttachmentSequence ();
167+ if (sequence > 0 ) {
168+ Database db = revision .getDatabase ();
169+ //Attachment attachment = db.getAttachmentForSequence(sequence, this.name);
170+ String path = db .getAttachmentPathForSequence (sequence , this .name );
148171 if (path != null ) {
149172 return new File (path ).toURI ().toURL ();
150173 }
151174 }
152- }
153- catch (Exception e ){
175+ } catch (Exception e ){
154176 Log .d (Log .TAG_DATABASE , e .getMessage ());
155177 }
156178 return null ;
0 commit comments