/
writeFileAnnotation.m
111 lines (96 loc) · 3.92 KB
/
writeFileAnnotation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
function fa = writeFileAnnotation(session, filePath, varargin)
% WRITEFILEANNOTATION Upload a file and create a file annotation on the OMERO server
%
% fa = writeFileAnnotation(session, filePath) uploads the file specified
% by filePath and creates a file annotation owned by the session user.
%
% fa = writeFileAnnotation(session, filePath, 'mimetype', mimetype) also
% specifies the mimetype of the file.
%
% fa = writeFileAnnotation(session, filePath, 'namespace', namespace)
% also sets the namespace of the file annotation.
%
% fa = writeFileAnnotation(session, filePath, 'description',
% description) also sets the description of the file annotation.
%
% Examples:
%
% fa = writeFileAnnotation(session, filePath)
% fa = writeFileAnnotation(session, filePath, 'mimetype', mimetype)
% fa = writeFileAnnotation(session, filePath, 'description',
% description)
%
% See also: WRITETEXTANNOTATION
% Copyright (C) 2013 University of Dundee & Open Microscopy Environment.
% All rights reserved.
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
% Input check
ip = inputParser;
ip.addRequired('session');
ip.addRequired('filePath', @(x) exist(x, 'file') == 2);
ip.addParamValue('mimetype', '', @ischar);
ip.addParamValue('namespace', '', @ischar);
ip.addParamValue('description', '', @ischar);
ip.parse(session, filePath, varargin{:});
% Create java io File
[~, f] = fileattrib(filePath);
[path, name, ext] = fileparts(f.Name);
fileLength = length(java.io.File(filePath));
% Create original file
originalFile = omero.model.OriginalFileI;
originalFile.setName(rstring([name ext]));
originalFile.setPath(rstring(path));
originalFile.setSize(rlong(fileLength));
originalFile.setHasher(omero.model.ChecksumAlgorithmI());
originalFile.getHasher.setValue(rstring('SHA1-160'));
if ~isempty(ip.Results.mimetype),
originalFile.setMimetype(rstring(ip.Results.mimetype));
end
% now we save the originalFile object
updateService = session.getUpdateService();
originalFile = updateService.saveAndReturnObject(originalFile);
% Initialize the service to load the raw data
rawFileStore = session.createRawFileStore();
rawFileStore.setFileId(originalFile.getId().getValue());
% Initialize provider to compute client-side checksum
checksumProviderFactory = ome.util.checksum.ChecksumProviderFactoryImpl;
sha1= ome.util.checksum.ChecksumType.SHA1;
hasher = checksumProviderFactory.getProvider(sha1);
%code for small file.
fid = fopen(f.Name);
byteArray = fread(fid,[1, fileLength], 'uint8');
rawFileStore.write(byteArray, 0, fileLength);
hasher.putBytes(byteArray);
fclose(fid);
% Save and close the service
originalFile = rawFileStore.save();
rawFileStore.close();
% Compare checksums client-side and server-sid
clientHash = char(hasher.checksumAsString());
serverHash = char(originalFile.getHash().getValue());
msg = 'File checksum mismatch on upload: %s (client has %s, server has %s)';
assert(isequal(clientHash, serverHash), msg, f.Name, clientHash, serverHash);
% Create a file annotation
fa = omero.model.FileAnnotationI;
fa.setFile(originalFile);
if ~isempty(ip.Results.description),
fa.setDescription(rstring(ip.Results.description));
end
if ~isempty(ip.Results.namespace),
fa.setNs(rstring(ip.Results.namespace))
end
% Save the file annotation
fa = updateService.saveAndReturnObject(fa);