<divid="dev_page_content"><p>File references are strings of bytes, that can be encountered in the <code>file_reference</code> fields of <ahref="/constructor/document">document</a> and <ahref="/constructor/photo">photo</a> objects.</p>
<p>They must be cached by the client, along with the <strong>origin context</strong> where the document/photo object was found, in order to be refetched when the file reference expires.</p>
<p>Example implementation of a reference database: <ahref="https://github.com/danog/MadelineProto/blob/master/src/danog/MadelineProto/MTProtoTools/ReferenceDatabase.php">MadelineProto</a>, <ahref="https://github.com/DrKLO/Telegram/blob/master/TMessagesProj/src/main/java/org/telegram/messenger/FileRefController.java">android</a>, <ahref="https://github.com/telegramdesktop/tdesktop/blob/bec39d89e19670eb436dc794a8f20b657cb87c71/Telegram/SourceFiles/data/data_file_origin.cpp">telegram desktop</a>, <ahref="https://github.com/tdlib/td/blob/56163c2460a65afc4db2c57ece576b8c38ea194b/td/telegram/FileReferenceManager.cpp">tdlib</a>.</p>
<p>Assume you receive a <ahref="/constructor/message">message</a> from your friend: that message contains a <ahref="/constructor/messageMediaPhoto">messageMediaPhoto</a> with a <ahref="/constructor/photo">photo</a>.</p>
<p>Your client has to cache not only the <code>file_reference</code> field of the photo, but also the context in which the file reference was seen (in this case, a message coming from a specific user).</p>
<p>The context info is in this case, <ahref="https://github.com/danog/MadelineProto/blob/master/src/danog/MadelineProto/MTProtoTools/ReferenceDatabase.php#L74">an origin context of type message</a>, containing the message ID and the peer ID of the chat/channel/user where the message was seen.</p>
<p>The context info has to be associated with the file reference: when downloading a file using <ahref="/method/upload.getFile">upload.getFile</a>, a <code>FILE_REFERENCE_EXPIRED</code> error (or another error starting with <code>FILE_REFERENCE_</code>) may be returned.<br>
If this happens, the context info must be used to refetch the object that contained the file reference: in this example, the peer info and the message ID have to be used with <ahref="/method/channels.getMessages">channels.getMessages</a> or <ahref="/method/messages.getMessages">messages.getMessages</a> to <ahref="https://github.com/danog/MadelineProto/blob/master/src/danog/MadelineProto/MTProtoTools/ReferenceDatabase.php#L481">refetch the message</a>, recache the file reference and use it in a new file download request.</p>
<p>More than one origin context can be associated to one file reference, for greater resilience (in the case of a message that was deleted in one chat but was also forwarded in another chat, the file reference can be refetched from the second chat, instead).</p>
<p>Origin contexts for objects returned by method calls with certain parameters can be considered, too (for example, in the case of favorited sticker sets returned by <ahref="/method/messages.getFavedStickers">messages.getFavedStickers</a>).</p></div>
</div>
</div>
</div>
<divclass="footer_wrap">
<divclass="footer_columns_wrap footer_desktop">
<divclass="footer_column footer_column_telegram">
<h5>Telegram</h5>
<divclass="footer_telegram_description"></div>
Telegram is a cloud-based mobile and desktop messaging app with a focus on security and speed.