public static IFileChunkHelper GetInstance(string path) { if (helpers == null) { InitHelpers(); } IFileChunkHelper rv = null; if (path != null) { string ext = System.IO.Path.GetExtension(path); if (ext != null) { foreach (IFileChunkHelper ch in helpers) { if (ch.Supports(ext)) { rv = ch; break; } } } } return(rv); }
// add a file to the configuration, with running chunk sizes if set public bool add(string fileName) { lock (myLock) { FileInfo fi = new FileInfo(fileName); long fsize = fi.Length; long deductible = ((totalBytes + deductedBytes) % chunkSize); // don't make a too-small first chunk if (deductible > ((1.0 - this.Config.MinimumChunk) * chunkSize)) { deductedBytes += (chunkSize - deductible); deductible = 0; } //// dont make a too-small last chunk ??????? [the calc is wrong, but so is the concept... doing this leads to possible very-short fills] //else if ((fsize + deductible) % chunkSize < (this.Config.MinimumChunk * chunkSize)) //{ // deductedBytes += deductible; // deductible = 0; //} chunks = (int)((deductible + fsize) / chunkSize); if (((chunks * chunkSize) - deductible) < fsize) { chunks += 1; } debug("exposing file " + fileName + " in chunks of " + chunkSize + "(total of " + chunks + ")"); for (int i = 0; i < chunks; i++) { long offset = (i * chunkSize) - deductible; long length; if (i == 0) { offset = 0; length = chunkSize - deductible; if (length > fsize) { length = fsize; } } else if (i + 1 == chunks) { length = (fsize + deductible) - (i * chunkSize); } else { length = chunkSize; } if (Config.UseExtension && Config.SmartChunk && i + 1 < chunks) { IFileChunkHelper ifh = FileChunkHelperFactory.GetInstance(fileName); if (ifh != null) { long suggested = (ifh.LocateChunkEndPoint(fileName, offset + length)); if (suggested < (offset + length)) { long delta = (offset + length) - suggested; length -= delta; deductible += delta; deductedBytes += delta; } } } Chunk c = new Chunk(fileName, offset, length, i + 1, chunks, Config.UseExtension); MyDirectory.Add(c.getFileName(), c); orderedChunks.Add(c); //if (currentSubdir != null) currentSubdir.Add(c); addToSubdir(c); debug("chunk added: " + c.getFileName()); totalBytes += c.LogicalLength; } return(chunks >= 1); } }