Ejemplo n.º 1
0
        public override bool Equals(object o)
        {
            if (!(o is Couchbase.Lite.BlobKey))
            {
                return(false);
            }

            Couchbase.Lite.BlobKey oBlobKey = (Couchbase.Lite.BlobKey)o;

            if (GetBytes() == null || oBlobKey.GetBytes() == null)
            {
                return(false);
            }

            return(Arrays.Equals(GetBytes(), oBlobKey.GetBytes()));
        }
Ejemplo n.º 2
0
        public bool StoreBlob(byte[] data, BlobKey outKey)
        {
            BlobKey newKey = KeyForBlob(data);

            outKey.SetBytes(newKey.GetBytes());
            string   path = PathForKey(outKey);
            FilePath file = new FilePath(path);

            if (file.CanRead())
            {
                return(true);
            }
            FileOutputStream fos = null;

            try
            {
                fos = new FileOutputStream(file);
                fos.Write(data);
            }
            catch (FileNotFoundException e)
            {
                Log.E(Database.Tag, "Error opening file for output", e);
                return(false);
            }
            catch (IOException ioe)
            {
                Log.E(Database.Tag, "Error writing to file", ioe);
                return(false);
            }
            finally
            {
                if (fos != null)
                {
                    try
                    {
                        fos.Close();
                    }
                    catch (IOException)
                    {
                    }
                }
            }
            // ignore
            return(true);
        }
Ejemplo n.º 3
0
        public bool StoreBlobStream(InputStream inputStream, BlobKey outKey)
        {
            FilePath tmp = null;

            try
            {
                tmp = FilePath.CreateTempFile(TmpFilePrefix, TmpFileExtension, new FilePath(this.path)
                                              );
                FileOutputStream fos    = new FileOutputStream(tmp);
                byte[]           buffer = new byte[65536];
                int lenRead             = inputStream.Read(buffer);
                while (lenRead > 0)
                {
                    fos.Write(buffer, 0, lenRead);
                    lenRead = inputStream.Read(buffer);
                }
                inputStream.Close();
                fos.Close();
            }
            catch (IOException e)
            {
                Log.E(Database.Tag, "Error writing blog to tmp file", e);
                return(false);
            }
            BlobKey newKey = KeyForBlobFromFile(tmp);

            outKey.SetBytes(newKey.GetBytes());
            string   path = PathForKey(outKey);
            FilePath file = new FilePath(path);

            if (file.CanRead())
            {
                // object with this hash already exists, we should delete tmp file and return true
                tmp.Delete();
                return(true);
            }
            else
            {
                // does not exist, we should rename tmp file to this name
                tmp.RenameTo(file);
            }
            return(true);
        }
Ejemplo n.º 4
0
 /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
 internal void InsertAttachmentForSequenceWithNameAndType(long sequence, string name, string contentType, int revpos, BlobKey key)
 {
     try
     {
         var args = new ContentValues(); // TODO: Create Add override and refactor to use initializer syntax.
         args["sequence"] = sequence;
         args["filename"] = name;
         if (key != null)
         {
             args.Put("key", key.GetBytes());
             args.Put("length", Attachments.GetSizeOfBlob(key));
         }
         args["type"] = contentType;
         args["revpos"] = revpos;
         var result = StorageEngine.Insert("attachments", null, args);
         if (result == -1)
         {
             var msg = "Insert attachment failed (returned -1)";
             Log.E(Tag, msg);
             throw new CouchbaseLiteException(msg, StatusCode.InternalServerError);
         }
     }
     catch (SQLException e)
     {
         Log.E(Tag, "Error inserting attachment", e);
         throw new CouchbaseLiteException(StatusCode.InternalServerError);
     }
 }
Ejemplo n.º 5
0
        public virtual string SHA1DigestString()
        {
            string base64Sha1Digest = Convert.ToBase64String(blobKey.GetBytes());

            return(string.Format("sha1-{0}", base64Sha1Digest));
        }
Ejemplo n.º 6
0
 public string PathForKey(BlobKey key)
 {
     return path + FilePath.separator + BlobKey.ConvertToHex(key.GetBytes()) + FileExtension;
 }
Ejemplo n.º 7
0
		public void InsertAttachmentForSequenceWithNameAndType(long sequence, string name
			, string contentType, int revpos, BlobKey key)
		{
			try
			{
				ContentValues args = new ContentValues();
				args.Put("sequence", sequence);
				args.Put("filename", name);
				if (key != null)
				{
					args.Put("key", key.GetBytes());
					args.Put("length", attachments.GetSizeOfBlob(key));
				}
				args.Put("type", contentType);
				args.Put("revpos", revpos);
				long result = database.Insert("attachments", null, args);
				if (result == -1)
				{
					string msg = "Insert attachment failed (returned -1)";
					Log.E(Database.Tag, msg);
					throw new CouchbaseLiteException(msg, Status.InternalServerError);
				}
			}
			catch (SQLException e)
			{
				Log.E(Database.Tag, "Error inserting attachment", e);
				throw new CouchbaseLiteException(e, Status.InternalServerError);
			}
		}
        public virtual string SHA1DigestString()
        {
            string base64Sha1Digest = Base64.EncodeBytes(blobKey.GetBytes());

            return(string.Format("sha1-%s", base64Sha1Digest));
        }
Ejemplo n.º 9
0
 public string PathForKey(BlobKey key)
 {
     return(path + FilePath.separator + BlobKey.ConvertToHex(key.GetBytes()) + FileExtension);
 }